Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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:32] – [CUDA] admincuda [2017/11/22 09:04] (Version actuelle) – [Exemple de programme] admin
Ligne 27: Ligne 27:
  
 <code bash> <code bash>
-$ module load cuda+$ module load gpu/cuda
 </code> </code>
  
Ligne 34: Ligne 34:
 ===== Exemple de programme ===== ===== Exemple de programme =====
  
-<file C VectorAdd.c>+<file C VectorAdd.cu>
  
 #include <stdio.h> #include <stdio.h>
-#include <cuda.h> 
 #include <stdlib.h> #include <stdlib.h>
 +#include <math.h>
  
-#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 *a,int *b, int *c) { +   /* block_count*threads_per_block may be >= n */ 
-        int tid = blockIdx.x *  blockDim.x + threadIdx.x; +   if (i < n) z[i] = x[i] + y[i]; 
-        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],b[N],c[N]; 
-        int *dev_a, *dev_b, *dev_c; 
  
-        printf("Size of array = %d\n", N); +/* Host code */ 
-        do { +int main(int argc, char* argv[]) { 
-                printf("Enter number of threads per block (1024 max, comp. cap. 2.x)  "); +   int n, i; 
-                scanf("%d",&T); +   float *h_x, *h_y, *h_z; 
-                printf("\nEnter nuumber of blocks per grid: "); +   float *d_x, *d_y, *d_z; 
-                scanf("%d",&B); +   int threads_per_block; 
-                if (T * B < N) printf("Error T x B < N, try again\n"); +   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, "usage: %s <vector order>\n", argv[0]); 
 +      exit(0); 
 +   } 
 +   n = strtol(argv[1], NULL, 10); 
 +   size = n*sizeof(float);
  
-       cudaSetDevice(0); +   /* Allocate input vectors in host memory */ 
-        cudaMalloc((void**)&dev_a,N * sizeof(int)); +   h_x = (float*) malloc(size); 
-        cudaMalloc((void**)&dev_b,N * sizeof(int)); +   h_y = (float*) malloc(size); 
-        cudaMalloc((void**)&dev_c,N * sizeof(int));+   h_z = (float*) malloc(size); 
 +    
 +   /* Initialize input vectors */ 
 +   for (i = 0; i < n; i++) { 
 +      h_x[i] = i+1; 
 +      h_y[i] = n-i; 
 +   }
  
-        for(int i=0;i<N;i++) {    // load arrays with some numbers +   printf("h_x = "); 
-                a[i] = i; +   for (i = 0; i < n; i++) 
-                b[i] = i; +      printf("%.1f ", h_x[i]); 
-        }+   printf("\n");
  
-        cudaMemcpy(dev_a, a , N*sizeof(int),cudaMemcpyHostToDevice); +   printf("h_y = "); 
-        cudaMemcpy(dev_b, b , N*sizeof(int),cudaMemcpyHostToDevice); +   for (i = 0; i < n; i++) 
-        cudaMemcpy(dev_c, c , N*sizeof(int),cudaMemcpyHostToDevice);+      printf("%.1f ", h_y[i]); 
 +   printf("\n\n");
  
-        cudaEventCreate( &start );     // instrument code to measure start time +   /* Allocate vectors in device memory */ 
-        cudaEventCreate( &stop ); +   cudaMalloc(&d_x, size); 
-        cudaEventRecord( start, 0 );+   cudaMalloc(&d_y, size); 
 +   cudaMalloc(&d_z, size);
  
-        vectorAdd<<<B,T>>>(dev_a,dev_b,dev_c);+   /* Copy vectors from host memory to device memory */ 
 +   cudaMemcpy(d_x, h_x, size, cudaMemcpyHostToDevice); 
 +   cudaMemcpy(d_y, h_y, size, cudaMemcpyHostToDevice);
  
-        cudaMemcpy(c,dev_c,N*sizeof(int),cudaMemcpyDeviceToHost);+   /* Define block size */ 
 +   threads_per_block = 256;
  
-        cudaEventRecord( stop, 0 );     // instrument code to measue end time +   /* Define grid size.  If we just computed n/threads_per_block */ 
-        cudaEventSynchronize( stop ); +   /* we might get fewer threads than vector components.  Using  */ 
-        cudaEventElapsedTime( &elapsed_time_ms, start, stop );+   /* ceil(n/threads_per_block) guarantees at least one thread   */ 
 +   /* per vector component.  The following formula is a kludge   */ 
 +   /* since it appears that the CUDA ceil function doesn't work  */ 
 +   /* correctly.                                                 */ 
 +   block_count = (n + threads_per_block - 1)/threads_per_block;
  
-        for(int i=0;i<N;i++) { +   /* Invoke kernel using block_count blocks, each of which  */ 
-                printf("%d+%d=%d\n",a[i],b[i],c[i]); +   /* contains threads_per_block threads                     */ 
-        }+   Vec_add<<<block_count, threads_per_block>>>(d_x, d_y, d_z, n);
  
-        printf("Time to calculate results: %f ms.\n", elapsed_time_ms);  // print out execution time+   /* Wait for the kernel to complete */ 
 +   cudaThreadSynchronize();
  
-        // clean up +   /* Copy result from device memory to host memory */ 
-        cudaFree(dev_a); +   /* h_z contains the result in host memory        */ 
-        cudaFree(dev_b); +   cudaMemcpy(h_z, d_z, size, cudaMemcpyDeviceToHost);
-        cudaFree(dev_c);+
  
-        cudaEventDestroy(start); +   printf("The sum is: \n"); 
-        cudaEventDestroy(stop);+   for (i = 0; i < n; i++) 
 +      printf("%.1f ", h_z[i]); 
 +   printf("\n");
  
-        return 0; +   /* Free device memory */ 
-}+   cudaFree(d_x); 
 +   cudaFree(d_y); 
 +   cudaFree(d_z); 
 + 
 +   /* Free host memory */ 
 +   free(h_x); 
 +   free(h_y); 
 +   free(h_z); 
 + 
 +   return 0; 
 +}  /* main */
  
 </file> </file>
Ligne 130: Ligne 160:
 <file bash cuda.sge> <file bash cuda.sge>
  
-#!/bin/bash -l +#!/bin/bash 
-      +
-#$ -l h_rt=2:00:0+
            
        
Ligne 144: 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
        
-    +   
-      +./monprogramme  
-    ################################# +
-    ###  Pour executer  #### +
-    ################################# +
- +
-     ./monprogramme  +
  
 </file> </file>
  
  • cuda.1461072754.txt.gz
  • Dernière modification : 2016/04/19 13:32
  • de admin