Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as C by registered user Mr_T ( 3 years ago )
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

//kernel
__global__ void add(int x, int y, int* z) {
	*z = x + y;
}

//programa principal
int main(int argc, char** argv) {
	int a, b, c;
	a = 2;
	b = 7;

	//punteros a variables en CPU 

	/*DESCOMENTAR PARA USAR EL ADD QUE NO FUNCIONA
	int* a_h = &a;
	int* b_h = &b;*/

	int* c_h = &c;

	//punteros a variables en GPU

	/*DESCOMENTAR PARA USAR EL ADD QUE NO FUNCIONA
	int* a_d = NULL;
	int* b_d = NULL;*/

	int* c_d = NULL;

	//reservamos memoria en GPU

	/*DESCOMENTAR PARA USAR EL ADD QUE NO FUNCIONA
	//cudaMalloc(&a_d, (1 * sizeof(int)));
	//cudaMalloc(&b_d, (1 * sizeof(int)));*/

	cudaMalloc(&c_d, (1 * sizeof(int)));

	//transferimos variables a la GPU

	/*DESCOMENTAR PARA USAR EL ADD QUE NO FUNCIONA
	cudaMemcpy(a_d, a_h, 1 * sizeof(int), cudaMemcpyHostToDevice);
	cudaMemcpy(b_d, b_h, 1 * sizeof(int), cudaMemcpyHostToDevice);*/

	cudaMemcpy(c_d, c_h, 1 * sizeof(int), cudaMemcpyHostToDevice);

	//lanzamos el kernel

	add << <1, 1 >> > (a, b, c_d); //ADD QUE FUNCIONA
	//add << <1, 1 >> > (*a_d, *b_d, c_d); //ADD QUE NO FUNCIONA

	////transferimos variables a la CPU

	/*cudaMemcpy(a_h, a_d, 1 * sizeof(int), cudaMemcpyDeviceToHost);
	cudaMemcpy(b_h, b_d, 1 * sizeof(int), cudaMemcpyDeviceToHost);*/

	cudaMemcpy(c_h, c_d, 1 * sizeof(int), cudaMemcpyDeviceToHost);

	printf("c=%d", *c_h);

	//liberar memoria de la CPU

	/*cudaFree(a_h);
	cudaFree(b_h);*/

	cudaFree(c_h);
	c_h = NULL;
}

 

Revise this Paste

Your Name: Code Language: