First the error:
error: a value of type "float *" cannot be used to initialize an entity of type "float (*)[2000]"
Heres the relevant code:
#define opType float
const int N = 2000;
opType *a_h,*b_h,*d_h, *a_d,*b_d, *d_d;
opType (*normalized_a)[N] = a_h;
opType (*normalized_b)[N] = b_h;
opType (*normalized_d)[N] = d_h;
opType (*normalized_a_d)[N] = a_d;
opType (*normalized_b_d)[N] = b_d;
opType (*normalized_d_d)[N] = d_d;
I am attempting to normalize it to two dimensions so that I can pass a double pointer to a function later on and reference the two dimensions using the shorthand bracket syntax. I am using CUDA so I must have one dimensional declarations for the copying of the memory. Is there any way to make the above work?
This construct is borrowed from http://forums.nvidia.com/ind开发者_运维百科ex.php?showtopic=83941&view=findpost&p=475901
int linear[10*32]; // linear array
int (*twodim)[32] = linear; // interpreted as a two-dimensional [10][32] array.
assert(&twodim[2][4] == &linear[2*32 + 4]); // these are the same
I don't see where the data for your array initialization comes from, but the error-message is quite clear: You declare 6 arrays, each of which contains 2000 float pointers. Of course you can't initialize one of these arrays with a single float pointer.
So in what form is your base data and how do you want to use it?
Edit: OK, based on your comment you have got something like
float myData[N][N];
filled with values. In this form you can already access it as myData[i][j]. Now, if you just cast it over to a float pointer like
float *myDataFlat = (float*)myData;
you can also access it through myDataFlat[i*N+j].
You can't initialise an array to a variable. You can initialise some members to a constant. In that case it would be:
opType (*normalized_a)[N] = {0}; /* all members 0 */
or you can use a loop or memset.
I am not an expert in C; i guess that the error is because of type mismatch (like it would have been in C++).
I think you have to do some casting on assignment. To make it readable you have to introduce a typedef:
typedef float opType;
const int N = 2000;
typedef opType (*TPointerToArray)[N]; // may be defined outside of a function
opType *a_h; // no value assigned yet
TPointerToArray normalized_a; // no value assigned yet
void MyCode()
{
...
a_h = ...; // i don't know how the value is assigned; it should be done first
...
normalized_a = (TPointerToArray)a_h; // "normalization"
normalized_a[100][200] = 900; // usage
...
}
I assume you are using global variables just for fun; it doesn't matter much.
In this code, technically, normalized_a is a pointer-to-array but i can think about it as an array-of-arrays or two-dimensional-array.
精彩评论