Hi,
We have the following STRUCT defined. As the number of properties is large, the total size of MyStruct1 can be more than 256 bytes.
typedef struct MyStruct1 {
double Value1;
double Value2;
int Value3;
double Value5[Value5Size];
int Value6Length;
double* Value6;
int Value7Length;
int* Value7;
int Struct2Length;
MyStruct2* Struct2;
double TotalValue;
bool IsGood;
};
typedef struct MyStruct2 {
double ValueA;
double ValueB;
double ValueC;
double ValueD;
double ValueE;
};
As shown in the code, the MyStruct2 has all of its members of Double. The MyStruct1 not only have member of type Double, it also has array of Double, array of Int, array of MyStruct2. These array members are represented by Pointer and another filed specifid the length.
The original data are marshalled from C#, and constructed as an ARRAY of MyStruct1 and each instance of MySTruct1 have array of int, double and MyStruct2 as the members.
The kernel is called from the host as shown below
for(int i=0; i<NumberOfSimualtion; i++)
{
for(int j=0; j<NumberOfSteps; j++)
{
kernel1<<<60,32>>>(mystruct1, ArrayofMyStruct1Length, ArrayofRandomNumbers);
}
}
and the kernel code is shown below.
__global__ void RunMonteCarlo(MyStruct1 *mystruct1, long ArrayofMyStruct1Length, double *randomnumbers)
{
int i = threadIdx.x + blockIdx.x * blockDim.x;
int stride = blockDim.x * gridDim.x;
while (i < ArrayofMyStruct1Length)
{
//work out the Value6 Return
for (int index = 0; index < mystruct1[i].Value6ength; index++)
{
Value6Return += mystruct1[i].Value6[index]*mystruct1[i].Value1*randomnumbers[1];
}
//work out the Value7 Return
for (int index = 0; index < mystruct1[i].Value7ength; index++)
{
Value7Return += mystruct1[i].Value7[index]*mystruct1[i].Value2*randomnumbers[2];
}
//work out the Struct2 Return
for(int p = 0; p < mystruct1[i].Struct2Length; p++)
{
MyStruct2 struct2 = mystruct1[i].Struct2[p];
struct2Return += struct2.ValueA * struct2.ValueB * randomnumbers[3];
}
mystruct1[i].TotalValue = Value6Return + Value7Return + struct2Return;
i += stride;
}
}
The kernel is currently having not very good running performance due to the way the data is structued.
In order to coalesce memroy access to global memory, should I break the ARRAY of STRUCT into STRUCT of ARRAY. As the MyStruct1’s members are not just simple double or integer, it also haa array of double, array of MyStruct2 and the length of them are different from instance to instance, how could I handle them?
Thanks