include "error.cuh" #include#include #define N 100 __global__ void add( int *a, int *b, int *c ) { int tid = blockIdx.x; // this thread handles the data at its thread id if (tid < N) c[tid] = a[tid] + b[tid]; } int main( void ) { int a[N], b[N], c[N]; int *dev_a, *dev_b, *dev_c; // allocate the memory on the GPU CHECK( cudaMalloc( (void**)&dev_a, N * sizeof(int) ) ); CHECK( cudaMalloc( (void**)&dev_b, N * sizeof(int) ) ); CHECK( cudaMalloc( (void**)&dev_c, N * sizeof(int) ) ); // fill the arrays 'a' and 'b' on the CPU for (int i=0; i >>( dev_a, dev_b, dev_c ); // copy the array 'c' back from the GPU to the CPU CHECK( cudaMemcpy( c, dev_c, N * sizeof(int), cudaMemcpyDeviceToHost ) ); // display the results for (int i=0; i julia集 在这里插入代码片任意长度矢量求和#include "error.cuh" #include#include #define N (33 * 1024) __global__ void add( int *a, int *b, int *c ) { //获取线程id,网格大小1D,线程块大小1D,gridDim.x=128,blockDim.x=128 //数据量:33792,线程数:16384 int tid = threadIdx.x + blockIdx.x * blockDim.x; //方法1: for(int i=0;i >>( dev_a, dev_b, dev_c ); //将数据从设备传递到主机上 CHECK( cudaMemcpy( c, dev_c, N * sizeof(int), cudaMemcpyDeviceToHost ) ); //结果校验 bool success = true; for (int i=0; i 矩阵处理
#define N 1000 #define BLOCK_SIZE 32 __managed__ int input_Matrix[N][N]; __managed__ int output_GPU[N][N]; __managed__ int output_CPU[N][N]; __managed__ int output_GPU[N][N]; __managed__ int output_CPU[N][N]; __global__ void kernel(int input_M[N][N], int output_M[N][N]) { int x = blockIdx.x * blockDim.x + threadIdx.x;//<矩阵转置 点积> int y = blockIdx.y * blockDim.y + threadIdx.y;// > //x维度变化快相当于内层循环 if(x >>(input_Matrix, output_GPU); CHECK(cudaDeviceSynchronize()); CHECK(cudaEventRecord(stop_gpu)); CHECK(cudaEventSynchronize(stop_gpu)); float elapsed_time_gpu; CHECK(cudaEventElapsedTime(&elapsed_time_gpu, start, stop_gpu)); printf("Time_GPU = %g ms.n", elapsed_time_gpu); CHECK(cudaEventDestroy(start)); CHECK(cudaEventDestroy(stop_gpu)); //校验 printf("n***********Check result**************n"); int ok=1; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { if(fabs(output_GPU[i][j] - output_CPU[i][j])>(1.0e-10)) { ok = 0; } } } if(ok) { printf("Pass!!!n"); } else { printf("Error!!!n"); } // free memory return 0; } 常量内存



