2d array testing in very simple code using CUDA

well. here my current situation…

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cutil_inline.h>

#define X 3
#define Y 4

int main(int argc, char *argv)
{
int i, j;
int **crray2d;
int **c_d;

crray2d = (int**)malloc(Xsizeof(int));
for(i=0; i<X; i++)
crray2d[i] = (int*)malloc(Y*sizeof(int));

CUDA_SAFE_CALL( cudaMalloc((void **) &c_d, Xsizeof(int)) );

for(i=0; i<X; i++)
CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

/* since i can not get pass this point, the following is not important at this time */

}

at this stage, i think i did what you suggested. but point of matter i think is that do you think i am using for loop to assign memory properly ?

for(i=0; i<X; i++)
CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

Many thanks one more time…

well. here my current situation…

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cutil_inline.h>

#define X 3
#define Y 4

int main(int argc, char *argv)
{
int i, j;
int **crray2d;
int **c_d;

crray2d = (int**)malloc(Xsizeof(int));
for(i=0; i<X; i++)
crray2d[i] = (int*)malloc(Y*sizeof(int));

CUDA_SAFE_CALL( cudaMalloc((void **) &c_d, Xsizeof(int)) );

for(i=0; i<X; i++)
CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

/* since i can not get pass this point, the following is not important at this time */

}

at this stage, i think i did what you suggested. but point of matter i think is that do you think i am using for loop to assign memory properly ?

for(i=0; i<X; i++)
CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

Many thanks one more time…

Look, this is a forum for discussing CUDA issues. What you’re struggling with at the moment and throughout this thread are not CUDA issues at all, those are generic software engineering issues. Specifically, in the above code you’re copying a 1d array into memory that hasn’t been previously allocated (e.g. being uninitialized c_d[0] points to a random location in device memory prior to invocation of cudaMemcpy(c_d[i],…))

I suggest you temporarily forget about CUDA and write a piece of code, that allocates a 2d array in memory, then initializes all the values of the array with incremental integers starting from your credit card number, then copies it to another 2d array, then re-initializes all the values of the second 2d array with the incremental integers starting from the expiration date on the card (as an integer) and then copies the second array back into the first array. Then make sure (using, e.g. valgrind) that you don’t leak memory and don’t step on unallocated memory. And yes, you should initialize the arrays with some weird different numbers, NOT zeros/ones/minus-ones, so that you can clearly see from your printouts that these weird numbers are being copied. I would write the above code as a function, invoke it from main(…) and thus have what we call “a unit test”.

Once you’ve done that host-only code (which means DEBUGGED it and confirmed that your printouts display expected values), then it won’t be too hard to modify the code to allocate the second array on the device and copy host-to-device, device-to-host instead of memcpy, as well as replace the initialization of the second array with a kernel, which you probably had right in your first version.

Look, this is a forum for discussing CUDA issues. What you’re struggling with at the moment and throughout this thread are not CUDA issues at all, those are generic software engineering issues. Specifically, in the above code you’re copying a 1d array into memory that hasn’t been previously allocated (e.g. being uninitialized c_d[0] points to a random location in device memory prior to invocation of cudaMemcpy(c_d[i],…))

I suggest you temporarily forget about CUDA and write a piece of code, that allocates a 2d array in memory, then initializes all the values of the array with incremental integers starting from your credit card number, then copies it to another 2d array, then re-initializes all the values of the second 2d array with the incremental integers starting from the expiration date on the card (as an integer) and then copies the second array back into the first array. Then make sure (using, e.g. valgrind) that you don’t leak memory and don’t step on unallocated memory. And yes, you should initialize the arrays with some weird different numbers, NOT zeros/ones/minus-ones, so that you can clearly see from your printouts that these weird numbers are being copied. I would write the above code as a function, invoke it from main(…) and thus have what we call “a unit test”.

Once you’ve done that host-only code (which means DEBUGGED it and confirmed that your printouts display expected values), then it won’t be too hard to modify the code to allocate the second array on the device and copy host-to-device, device-to-host instead of memcpy, as well as replace the initialization of the second array with a kernel, which you probably had right in your first version.

#define X 3
#define Y 4

int main(int argc, char *argv)
{
int i, j;
int **crray2d;
int **c_d;

crray2d = (int**)malloc(Xsizeof(int));
for(i=0; i<X; i++)
crray2d[i] = (int*)malloc(Y*sizeof(int));

CUDA_SAFE_CALL( cudaMalloc((void **) &c_d, Xsizeof(int)) );

for(i=0; i<X; i++)
CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

/* since i can not get pass this point, the next is not important at this time */

==============================================

yes… this is CUDA site and do you see something other than CUDA?

at this point, it’d be better if i can see at least one lime of code than your words…

the problem line is just one line

CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

what would you do for this line ?

#define X 3
#define Y 4

int main(int argc, char *argv)
{
int i, j;
int **crray2d;
int **c_d;

crray2d = (int**)malloc(Xsizeof(int));
for(i=0; i<X; i++)
crray2d[i] = (int*)malloc(Y*sizeof(int));

CUDA_SAFE_CALL( cudaMalloc((void **) &c_d, Xsizeof(int)) );

for(i=0; i<X; i++)
CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

/* since i can not get pass this point, the next is not important at this time */

==============================================

yes… this is CUDA site and do you see something other than CUDA?

at this point, it’d be better if i can see at least one lime of code than your words…

the problem line is just one line

CUDA_SAFE_CALL( cudaMemcpy(c_d[i], crray2d[i], Ysizeof(int), cudaMemcpyHostToDevice) );

what would you do for this line ?

I either don’t understand your code or it’s conceptually wrong. Hence I’m afraid I cannot help you with this. I sincerely wish you the best of luck!

I either don’t understand your code or it’s conceptually wrong. Hence I’m afraid I cannot help you with this. I sincerely wish you the best of luck!

well. thanks.

its just that you are so high level in this field and i am just a starter…

so for a person like me, a simple example would have been much better to understand your words.

i will keep looking other comments and your too. thanks for your help so far and i appreciate it a lot…

well. thanks.

its just that you are so high level in this field and i am just a starter…

so for a person like me, a simple example would have been much better to understand your words.

i will keep looking other comments and your too. thanks for your help so far and i appreciate it a lot…