Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting dynamic initialization not supported for __device__, __constant__, __shared__?

Tags:

cuda

cmake

I don't understand why am I getting the error dynamic initialization is not supported for __device__, __constant__, __shared__ variables when compiling my code. My code looks like

wrapper.cu

#include "../Params.hpp"

__constant__ Params cparams;

void wrapperFunction(uint& a)
{
   Params ab;
   a = 20;
}

Params.hpp

#include "Utils.hpp"

typedef struct Params
{
   vectorTypef a;
} Params;

Utils.hpp

#include "Vec2.hpp"

typedef unsigned int uint;
typedef Vec2<float> vectorTypef;

Vec2.hpp

template <typename T>
class Vec2
{
public:
   Vec2(){ x = 0.0; y = 0.0;}
   T x, y;
};

Building with cmake with the command

CUDA_ADD_EXECUTABLE(test main.cpp cudasrc/wrapper.cu

like image 932
BRabbit27 Avatar asked Nov 20 '14 14:11

BRabbit27


People also ask

How do you dynamically initialize a variable?

Dynamic initialization can be achieved using constructors and passing parameters values to the constructors. This type of initialization is required to initialize the class variables during run time.

What is dynamic initialisation in C++?

Dynamic initialization of object in C++ Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time. It can be achieved by using constructors and by passing parameters to the constructors.

What are variables explain dynamic initialization with example?

The process of initializing a variable at the moment it is declared at runtime is called dynamic initialization of the variable. Thus, during the dynamic initialization of a variable, a value is assigned to execution when it is declared. Example: main() { Int a; cout<<“Enter Value of a”; cin>>a; int cube = a * a * a; }


1 Answers

Your Params struct is used in the __constant__ memory definition of cparams.

Your Params struct contains an element a of type vectorTypef which is a typedef for the Vec2 class for float. This class has a default constructor, that is assigning elements ultimately of the Params struct. This method of assigning data to a __constant__ region is not legal either in device code or host code.

In device code it's not legal to modify a __constant__ value at all. In host code (which is what is in view here), __constant__ values should be assigned using the appropriate API, i.e. cudaMemcpyToSymbol. I would recommend that you assign these in your host code explicitly, rather than via a constructor.

So, one possible approach to fix this would be to change your default constructor to an empty one:

public:
   __host__ __device__ Vec2(){ }; // change this line
   T x, y;

(you could also just delete the empty default constructor line)

And, in wrapper.cu (perhaps in wrapperFunction), initialize your Params __constant__ struct:

Params hparams;
hparams.a.x = 0.0;
hparams.a.y = 0.0;
cudaMemcpyToSymbol(cparams, &hparams, sizeof(Params));
like image 149
Robert Crovella Avatar answered Sep 21 '22 12:09

Robert Crovella