1 准备工作
1)安装 xshell
2)远程连接服务器,打开 jupyter 服务
3)网页进入对应的 jupyter 服务
2 Launcher 介绍
3 查看 GPU 设备信息
1)点击 Text File
2)在其中输入对应的代码
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
int main()
{
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0);
printf("设备名称与型号: %s\n", deviceProp.name);
printf("显存大小: %d MB\n", (int)(deviceProp.totalGlobalMem / 1024 / 1024));
printf("含有的SM数量: %d\n", deviceProp.multiProcessorCount);
printf("CUDA CORE数量: %d\n", deviceProp.multiProcessorCount * 32);
printf("计算能力: %d.%d\n", deviceProp.major, deviceProp.minor);
}
3)重命名文件,后缀名改为 .cu
4)在 Terminal 中输入以下指令编译代码
nvcc filename.cu -o filename.out
5)在 Terminal 中输入一下指令运行可执行文件
./filename.out
4 向量加法的实现
1)点击 Text File
2)在其中输入对应的代码 CPU version
#include <stdio.h>
#include <time.h>
const int N = 50000;
int main()
{
clock_t start, end;
start = clock();
float a[N], b[N], c[N];
for (int i = 0; i < N; i ++) a[i] = 0, b[i] = 0;
for (int i = 0; i < N; i ++) c[i] = a[i] + b[i];
end = clock();
printf("Vector addition on CPU use %.8f s.\n", (float)(end - start) / CLOCKS_PER_SEC);
}
CUDA version
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
const int N = 50000;
__global__ void additionKernelVersion1(float*, float*, float*, const int);
__global__ void additionKernelVersion2(float*, float*, float*, const int);
__global__ void additionKernelVersion3(float*, float*, float*, const int);
int main()
{
clock_t start, end;
start = clock();
float a[N], b[N], c[N];
for (int i = 0; i < N; i ++) a[i] = 0, b[i] = 0;
float *device_a, *device_b, *device_c = NULL;
cudaMalloc((void**)&device_a, sizeof(float) * size);
cudaMalloc((void**)&device_b, sizeof(float) * size);
cudaMalloc((void**)&device_c, sizeof(float) * size);
cudaMemcpy(device_a, a, sizeof(float) * size, cudaMemcpyHostToDevice);
cudaMemcpy(device_b, b, sizeof(float) * size, cudaMemcpyHostToDevice);
additionKernelVersion1<<<ceil(N / 32), 32>>>(device_a, device_b, device_c, size);
cudaMemcpy(device_c, c, sizeof(float) * size, cudaMemcpyDeviceToHost);
cudaFree(device_a);
cudaFree(device_b);
cudaFree(device_c);
end = clock();
printf("Vector addition version 1 on GPU use %.8f s.\n", (float)(end - start) / CLOCKS_PER_SEC);
}
__global__ void additionKernelVersion1(float* A, float* B, float* C, const int size)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
C[i] = A[i] + B[i];
}
__global__ void additionKernelVersion2(float* A, float* B, float* C, const int size)
{
int i = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
C[i] = A[i] + B[i];
C[i + 1] = A[i + 1] + B[i + 1];
}
__global__ void additionKernelVersion3(float* A, float* B, float* C, const int size)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
C[i] = A[i] + B[i];
C[i + blockDim.x] = A[i + blockDim.x] + B[i + blockDim.x];
}
3)重命名文件,后缀名改为 .cu
4)在 Terminal 中输入以下指令编译代码
nvcc vector_addition.cu -o vector_addition.out
5)在 Terminal 中输入一下指令运行可执行文件
./vector_addition.out
|