代码拉取完成,页面将自动刷新
#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>
//Definition of kernel function to add two variables
__global__ void gpuAdd(int d_a, int d_b, int *d_c) {
*d_c = d_a + d_b;
}
//main function
int main(void) {
//Defining host variable to store answer
int h_c;
//Defining device pointer
int *d_c;
//Allocating memory for device pointer
cudaMalloc((void**)&d_c, sizeof(int));
//Kernel call by passing 1 and 4 as inputs and storing answer in d_c
//<< <1,1> >> means 1 block is executed with 1 thread per block
gpuAdd << <1, 1 >> > (1, 4, d_c);
//Copy result from device memory to host memory
cudaMemcpy(&h_c, d_c, sizeof(int), cudaMemcpyDeviceToHost);
printf("1 + 4 = %d\n", h_c);
//Free up memory
cudaFree(d_c);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。