[Solved] vector addition in CUDA using streams


One problem is how you are handling h_A, h_B, and h_C:

h_A = (float *) wbImport(wbArg_getInputFile(args, 0), &inputLength);
h_B = (float *) wbImport(wbArg_getInputFile(args, 1), &inputLength);

The above lines of code are creating an allocation for h_A and h_B and importing some data (presumably).

These lines of code:

cudaHostAlloc((void **) &h_A, size, cudaHostAllocDefault);
cudaHostAlloc((void **) &h_B, size, cudaHostAllocDefault);
cudaHostAlloc((void **) &h_C, size, cudaHostAllocDefault);

Are not doing what you think. They are creating a new allocation for h_A, h_B and h_C. Whatever data those pointers previously referenced is no longer accessible from those pointers (i.e. for all intents and purposes, it is lost).

CUDA should be able to work just fine with the pointers and allocations being created here:

h_A = (float *) wbImport(wbArg_getInputFile(args, 0), &inputLength);
h_B = (float *) wbImport(wbArg_getInputFile(args, 1), &inputLength);
h_C = (float *) malloc(inputLength * sizeof(float));

So delete these lines of code:

cudaHostAlloc((void **) &h_A, size, cudaHostAllocDefault);
cudaHostAlloc((void **) &h_B, size, cudaHostAllocDefault);
cudaHostAlloc((void **) &h_C, size, cudaHostAllocDefault);

and delete these:

cudaFreeHost(h_A);
cudaFreeHost(h_B);
cudaFreeHost(h_C);

And you should be closer to a solution.

1

solved vector addition in CUDA using streams