Okay so I'm trying to learn CUDA for the 'new' FX 570 I bought for $15 ;D now in the code there are NO errors, the the array1_host starts off with it's values correctly, but when I copy the memory from device to host the values remain the same. the same thing happens if I blank out the second kernel call (trying multiple kernels in this project) I'm rather confused so thank you for any help I can achieve :)
#include <cuda_runtime.h>
#include <iostream>
#pragma comment (lib, "cudart")
#define N 5000
__global__ void addArray(float* a, float* b)
{
a[threadIdx.x] += b[threadIdx.x];
}
__global__ void timesArray(float* a, float* b)
{
a[threadIdx.x] *= b[threadIdx.x];
}
int main(){
float array1_host[N];
float array2_host[N];
float *array1_device;
float *array2_device;
cudaError_t err;
for(int x = 0; x < N; x++){
array1_host[x] = (float) x * 2;
array2_host[x] = (float) x * 6;
}
err = cudaMalloc((void**)&array1_device, N*sizeof(float));
err = cudaMalloc((void**)&array2_device, N*sizeof(float));
err = cudaMemcpy(array1_device, array1_host, N*sizeof(float), cudaMemcpyHostToDevice);
err = cudaMemcpy(array2_device, array2_host, N*sizeof(float), cudaMemcpyHostToDevice);
dim3 dimBlock( N );
dim3 dimGrid ( 1 );
addArray<<<dimGrid, dimBlock>>>(array1_device, array2_device);
timesArray<<<dimGrid, dimBlock>>>(array1_device, array2_device);
err = cudaMemcpy(array1_host, array1_device, N*sizeof(float), cudaMemcpyDeviceToHost);
cudaFree(array1_device);
cudaFree(array2_device);
std::cout << cudaGetErrorString(err) << "\n\n\n\n\n\n";
std::cout << array1_host;
cudaDeviceReset();
system("pause");
return 0;
}
You have an error, because N is 5000, but there are limits for threds in block - it depends on Compute Capability link to features on wiki .
Try this code:
#define K 200
....
dim3 dimBlock( K );
dim3 dimGrid ( N/K );
To debug your code you can use cudaGetLastError()
after each call of kernel or other function to know, where bugs are placed exaple about CUDA errors .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.