Hello.
I would like to allocate 2D array of arbitrary sizes to support coalescend access. I found the following example in C:
int numRows, numCols;
int **arrayPtr;
int rowIndex, colIndex;
printf("Enter values for numRows and numCols\n");
scanf("%d",&numRows);
scanf("%d",&numCols);
/* Create space for first array (array of pointers to each row). */
arrayPtr = (int **) malloc(numRows*sizeof(int));
if (arrayPtr == NULL)
{
printf("Not enough memory\n");
exit(1);
}
/* Loop through first array and create space for according to number of columns.*/
for(rowIndex=0;rowIndex < numRows;rowIndex++)
{
arrayPtr[rowIndex] = malloc(numCols*sizeof(int));
if (arrayPtr[rowIndex] == NULL)
{
printf("Not enough memory\n");
exit(1);
}
}
This can be directly translated into CudaMalloc I believe.
However, there’s one issue I’m afraid of. Namely, first allocation creates arrays of pointers. Later, subsequent allocations generate NEW array in a different memory space. Accessing this actual 2D array would require dereferencing pointers from first array, which can cause signinifacnt slowdown and cancel all benefits from coalescend access. Or am I wrong?
The question is:
How to allocate a consistent array which could be accessed directly by [row][column] indexes?