Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| cuda [2016/04/19 13:31] – admin | cuda [2017/11/22 09:04] (Version actuelle) – [Exemple de programme] admin | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== CUDA ====== | ====== CUDA ====== | ||
| + | Voici un exemple de comment compiler et exécuter un programme CUDA (somme de deux vecteurs) | ||
| ===== Mode interactif ===== | ===== Mode interactif ===== | ||
| Ligne 26: | Ligne 27: | ||
| <code bash> | <code bash> | ||
| - | $ module load cuda | + | $ module load gpu/cuda |
| </ | </ | ||
| Ligne 33: | Ligne 34: | ||
| ===== Exemple de programme ===== | ===== Exemple de programme ===== | ||
| - | <file C VectorAdd.c> | + | <file C VectorAdd.cu> |
| #include < | #include < | ||
| - | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| - | #define N 4096 // size of array | + | /* Kernel for vector addition */ |
| + | __global__ void Vec_add(float x[], float y[], float z[], int n) { | ||
| + | /* blockDim.x = threads_per_block | ||
| + | /* First block gets first threads_per_block components. | ||
| + | /* Second block gets next threads_per_block components, etc. */ | ||
| + | int i = blockDim.x * blockIdx.x + threadIdx.x; | ||
| - | __global__ void vectorAdd(int | + | /* block_count*threads_per_block may be >= n */ |
| - | int tid = blockIdx.x | + | |
| - | if(tid < N){ | + | } /* Vec_add */ |
| - | c[tid] = a[tid]+b[tid]; | + | |
| - | } | + | |
| - | } | + | |
| - | int main(int argc, char *argv[]) | ||
| - | int T = 10, B = 1; // threads per block and blocks per grid | ||
| - | int a[N], | ||
| - | int *dev_a, *dev_b, *dev_c; | ||
| - | printf("Size of array = %d\n", N); | + | /* Host code */ |
| - | do { | + | int main(int argc, char* argv[]) { |
| - | | + | int n, i; |
| - | | + | float *h_x, *h_y, *h_z; |
| - | | + | float *d_x, *d_y, *d_z; |
| - | | + | int threads_per_block; |
| - | if (T * B < N) printf(" | + | int block_count; |
| - | } while (T * B < N); | + | size_t size; |
| - | cudaEvent_t start, stop; // using cuda events to measure time | + | /* Get number of components in vector */ |
| - | float elapsed_time_ms; // which is applicable for asynchronous code also | + | if (argc != 2) { |
| + | fprintf(stderr, | ||
| + | exit(0); | ||
| + | } | ||
| + | n = strtol(argv[1], | ||
| + | size = n*sizeof(float); | ||
| - | | + | /* Allocate input vectors in host memory */ |
| - | | + | h_x = (float*) malloc(size); |
| - | | + | h_y = (float*) malloc(size); |
| - | | + | h_z = (float*) malloc(size); |
| + | |||
| + | /* Initialize input vectors | ||
| + | | ||
| + | h_x[i] = i+1; | ||
| + | h_y[i] = n-i; | ||
| + | } | ||
| - | | + | |
| - | a[i] = i; | + | for (i = 0; i < n; i++) |
| - | b[i] = i; | + | |
| - | } | + | printf(" |
| - | cudaMemcpy(dev_a, a , N*sizeof(int), | + | |
| - | | + | for (i = 0; i < n; i++) |
| - | | + | printf("%.1f ", h_y[i]); |
| + | printf(" | ||
| - | cudaEventCreate( &start ); // instrument code to measure start time | + | /* Allocate vectors in device memory */ |
| - | | + | |
| - | | + | cudaMalloc(&d_y, size); |
| + | cudaMalloc(&d_z, size); | ||
| - | vectorAdd<<< | + | /* Copy vectors from host memory to device memory */ |
| + | | ||
| + | | ||
| - | cudaMemcpy(c, | + | /* Define block size */ |
| + | | ||
| - | cudaEventRecord( stop, 0 ); // instrument code to measue end time | + | /* Define grid size. If we just computed n/ |
| - | | + | /* we might get fewer threads than vector components. |
| - | | + | /* ceil(n/ |
| + | /* per vector component. | ||
| + | /* since it appears that the CUDA ceil function doesn' | ||
| + | /* correctly. | ||
| + | | ||
| - | for(int i=0; | + | /* Invoke kernel using block_count blocks, each of which */ |
| - | | + | /* contains threads_per_block threads |
| - | } | + | |
| - | printf(" | + | /* Wait for the kernel |
| + | | ||
| - | | + | /* Copy result from device memory to host memory */ |
| - | | + | /* h_z contains the result in host memory |
| - | | + | cudaMemcpy(h_z, d_z, size, cudaMemcpyDeviceToHost); |
| - | cudaFree(dev_c); | + | |
| - | cudaEventDestroy(start); | + | |
| - | | + | for (i = 0; i < n; i++) |
| + | printf(" | ||
| + | | ||
| - | | + | /* Free device memory */ |
| - | } | + | |
| + | | ||
| + | | ||
| + | |||
| + | /* Free host memory */ | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | return 0; | ||
| + | } /* main */ | ||
| </ | </ | ||
| Ligne 129: | Ligne 160: | ||
| <file bash cuda.sge> | <file bash cuda.sge> | ||
| - | # | + | # |
| - | + | ||
| - | #$ -l h_rt=2:00:0 | + | |
| Ligne 143: | Ligne 172: | ||
| - | #$ -q tesla | + | #$ -q tesla.q |
| #$ -V | #$ -V | ||
| + | |||
| + | #$ -cwd | ||
| - | #$ -l h_vmem=1.5G | + | #$ -l h_vmem=2G |
| - | module load cuda | + | module load gpu/cuda |
| - | + | ||
| - | ################################# | + | |
| - | ### Pour compiler ### | + | |
| - | ################################# | + | |
| + | |||
| nvcc monprogramme.cu -o monprogramme | nvcc monprogramme.cu -o monprogramme | ||
| - | + | | |
| - | + | ./ | |
| - | ################################# | + | |
| - | ### Pour executer | + | |
| - | ################################# | + | |
| - | + | ||
| - | ./ | + | |
| </ | </ | ||