SlideServe
Menu
Préférences
© SlideServe · v2026

Programming With CUDA

kuame-simon avatar
6 diapositives · 233 Views · Nov 09, 2014

Chris Kerkhoff Matthew Sullivan 12/2/2009. Programming With CUDA. Basic Flow. The host computer initializes an array with data. The array is copied from the main memory to the memory on the GPU. The GPU performs operations on the array.

Programming With CUDA - slide 1
01 / 6Programming With CUDA
1 / 17

Télécharger la présentation

Programming With CUDA
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Chris Kerkhoff Matthew Sullivan 12/2/2009 Programming With CUDA

  2. Basic Flow • The host computer initializes an array with data. • The array is copied from the main memory to the memory on the GPU. • The GPU performs operations on the array. • The array is copied back to the main memory on the computer.

  3. #include<stdio.h> #include<cuda.h> // Kernel that executes on the CUDA device: __global__ voidcube_array(float *a, int N) {intidx = blockIdx.x * blockDim.x + threadIdx.x; if (idx<N) a[idx] = a[idx] * a[idx] * a[idx]; } int main(void) {// main routine that executes on the host float *a_h, *a_d; // Pointer to host & device arrays constint N = 10; // Number of elements in arrays size_t size = N * sizeof(float); a_h = (float *)malloc(size); // Allocate array on host cudaMalloc((void **) &a_d, size); // Allocate array on device // Initialize host array and copy it to CUDA device: for (inti=0; i<N; i++) a_h[i] = (float)i; cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice); // Do calculation on device: intblock_size = 4; intn_blocks = N/block_size + (N%block_size == 0 ? 0:1); cube_array <<< n_blocks, block_size >>> (a_d, N); // Retrieve result from device and store it in host array: cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost); for (inti=0; i<N; i++) printf("%d %f\n", i, a_h[i]); //Print results free(a_h); cudaFree(a_d);}//Cleanup

  4. 0 0.000000 1 1.000000 2 8.000000 3 27.000000 4 64.000000 5 125.000000 6 216.000000 7 343.000000 8 512.000000 9 729.000000 Press any key to continue . . .

  5. Only if you have an Nvidia card