To practice coding with CUDA, I made a little test scenario where I have three files:
memory.cHolds pureCcodememory_kernels.hDeclarations for CUDA kernels and functions to launch kernelsmemory_kernels.cuDefinitions of kernels
What the program should do is create an integer array on the host, copy it to the device and query the elements. The kernel will print out a few details.
However, I am getting the error:
Error in memory_kernels.cu at line 43 with error code "unspecified launch failure"
The source code for the three files are given below:
/**
* memory.c
*
* Test copying large arrays to device
* and printing from kernel
*/
/* Include standard libraries */
#include <stdlib.h>
#include <stdio.h>
/* Include local header files */
#include "memory_kernels.h"
int main() {
/* Size of array */
int i, N = 1024;
/* Array */
int *intArr = (int *) malloc( N * sizeof(int) );
/* Fill array */
for( i = 0; i < N; i++ ) {
intArr[i] = i;
}
/* Run CUDA code */
cuda_mem( &intArr );
/* Clean up device */
cudaDeviceReset();
/* Everything done */
exit(EXIT_SUCCESS);
}
/**
* memory_kernels.h
*
* Declarations for CUDA kernels
*/
/* Determine compiler */
#ifdef __cplusplus
#define EXTCFUNC extern "C"
#else
#define EXTCFUNC extern
#endif
#ifndef KERNELS_H
#define KERNELS_H
/* Standard libraries (only needed for debugging) */
#include <stdio.h>
/* Include CUDA header files */
#include <cuda.h>
#include <cuda_runtime.h>
#define CUDA_CALL(x) do { if((x) != cudaSuccess) { \
printf("Error in %s at line %d with error code \"%s\"\n",__FILE__,__LINE__,cudaGetErrorString(x)); \
exit(x);}} while(0)
/* Device globals */
__device__ int *d_intArr;
/* Device kernels */
__global__ void mem();
/* Host access functions */
EXTCFUNC void cuda_mem( int **intArr );
#endif
/**
* memory_kernels.cu
*
* CUDA kernel implementations
*/
/* Include header file */
#include "memory_kernels.h"
__global__ void mem() {
int i = threadIdx.x;
int a = d_intArr[i];
printf("i = %d a = %d\n",i,a);
}
/* Determine compiler */
#ifdef __cplusplus
#define EXTCFUNC extern "C"
#else
#define EXTCFUNC extern
#endif
/**
* cuda_mem()
*
* Test copying large array to device
* and printing from kernel
*/
EXTCFUNC void cuda_mem( int **intArr ) {
/* Local variables */
int N = 1024;
/* Initialise device variables */
CUDA_CALL( cudaMalloc( (void **) &d_intArr, sizeof(int) * N ) );
/* Copy to device initial values */
CUDA_CALL( cudaMemcpy( d_intArr, *intArr, sizeof(int) * N, cudaMemcpyHostToDevice ) );
/* Run kernel */
mem <<< 1,N >>> ();
CUDA_CALL( cudaPeekAtLastError() );
CUDA_CALL( cudaDeviceSynchronize() );
/* Free local scoped dynamically allocated memory */
CUDA_CALL( cudaFree( d_intArr ) );
}
Compilation is done with the following commands:
nvcc -c -o memory.o memory.c -arch=sm_20
nvcc -c -o memory_kernels.o memory_kernels.cu -arch=sm_20
nvcc -o memory memory.o memory_kernels.o -arch=sm_20
and ran on an NVIDIA Tesla M2050 with CUDA 4.0. Compute capability 2.0 is required to use printf() in the kernel.
Having searched around for a solution, the error code suggests I have a segmentation fault in the kernel, when reading from global memory. However, I am launching the same number of threads as the size of the array.
Having experimented around, I have a feeling the error is caused when copying intArr to the device. Maybe I am getting my pointers all mixed up?
I understand if the file structure is a bit strange, but it's all part of a larger program, but I have reduced the error to this smaller case.