学到了cudaMemcpyToSymbol
竟然还有将数据从host拷贝到global的功能,以前只用过这个函数拷贝到constant memory。拷贝方式的不同是由目的内存申请的方式决定的。
申请的是device内存,cudaMemcpyToSymbol
拷贝就是从host拷贝到global memory。
申请的是constant内存,cudaMemcpyToSymbol
拷贝就是从host拷贝到constant memory。
__device__ float g_damp_x[MAX_DIM];
__constant__ float c_damp_y[MAX_DIM];
main.cu
#include "error.cuh"
#include <stdio.h>
__device__ int d_x = 1;
__device__ int d_y[2];void __global__ my_kernel(void)
{d_y[0] += d_x;d_y[1] += d_x;printf("d_x = %d, d_y[0] = %d, d_y[1] = %d.\n", d_x, d_y[0], d_y[1]);
}int main(void)
{int h_y[2] = {10, 20};CHECK(cudaMemcpyToSymbol(d_y, h_y, sizeof(int) * 2));my_kernel<<<1, 1>>>();CHECK(cudaDeviceSynchronize());CHECK(cudaMemcpyFromSymbol(h_y, d_y, sizeof(int) * 2));printf("h_y[0] = %d, h_y[1] = %d.\n", h_y[0], h_y[1]);return 0;
}
#pragma once
#include <stdio.h>#define CHECK(call) \
do \
{ \const cudaError_t error_code = call; \if (error_code != cudaSuccess) \{ \printf("CUDA Error:\n"); \printf(" File: %s\n", __FILE__); \printf(" Line: %d\n", __LINE__); \printf(" Error code: %d\n", error_code); \printf(" Error text: %s\n", \cudaGetErrorString(error_code)); \exit(1); \} \
} while (0)
参考:
https://blog.csdn.net/u014800094/article/details/53994933?utm_source=blogxgwz5