Skip to content

Instantly share code, notes, and snippets.

@ShangjinTang
Last active August 30, 2022 23:42
Show Gist options
  • Save ShangjinTang/a1c9322c1fbfdbbaf9669aa85ef402ce to your computer and use it in GitHub Desktop.
Save ShangjinTang/a1c9322c1fbfdbbaf9669aa85ef402ce to your computer and use it in GitHub Desktop.
CUDA
#include <cstdio>
#include "cuda.h"
__global__
void loop_on_device()
{
printf("Device(GPU) iteration number %d\n", blockIdx.x * blockDim.x + threadIdx.x);
}
void loop_on_host(int length)
{
for (int i = 0; i < length; ++i) {
printf("Host(CPU) iteration number %d\n", i);
}
}
int main()
{
int block_size = 3;
int kernel_size = 4;
loop_on_device<<<block_size, kernel_size>>>();
int n = block_size * kernel_size;
loop_on_host(n);
cudaDeviceSynchronize();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment