Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- __global__ void suma(int* a, int* b, int* c)
- {
- int idThread = blockIdx.x;
- c[idThread] = a[idThread] + b[idThread];
- }
- __host__ __device__ void printHolaMundo()
- {
- printf("Hola mundo\n");
- }
- __global__ void holaMundo()
- {
- printHolaMundo();
- }
- int main(int argc, char** argv)
- {
- int i;
- int *a, *b, *c;
- int *d_a, *d_b, *d_c;
- int vSize = 10; //vector Size
- a = (int*)malloc(sizeof(int) * vSize);
- b = (int*)malloc(sizeof(int) * vSize);
- c = (int*)malloc(sizeof(int) * vSize);
- cudaMalloc((void **)&d_a, sizeof(int) * vSize);
- cudaMalloc((void **)&d_b, sizeof(int) * vSize);
- cudaMalloc((void **)&d_c, sizeof(int) * vSize);
- //inicializacion
- for ( i = 0; i < vSize; i++)
- {
- a[i] = b[i] = i;
- }
- cudaMemcpy(d_a, a, vSize * sizeof(int), cudaMemcpyHostToDevice);
- cudaMemcpy(d_b, b, vSize * sizeof(int), cudaMemcpyHostToDevice);
- cudaMemset(d, 0xFF, vSize * sizeof(int));
- h_c = (int*)malloc(sizeof(int));
- a = 10;
- b = 10;
- *h_c = 10;
- //cudaMemcpy( d_c, h_c, sizeof(int), cudaMemcpyHostToDevice);
- suma << <1, 1 >> > (a, b, d_c);
- cudaMemcpy(h_c, d_c, sizeof(int), cudaMemcpyDeviceToHost);
- printf("resultado: %d\n", *h_c);
- cudaFree(d_c);
- free(h_c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement