CUDA使用多维数组
程序员文章站
2022-07-12 20:02:43
...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define FILE_MAX 3
#define MOL_MAX 3
#define ATOM_MAX 3
#define XMAX 5
#define YMAX 5
#define ZMAX 5
typedef struct full {
int array[FILE_MAX][MOL_MAX][ATOM_MAX][ZMAX][YMAX][XMAX];
} full_t;
typedef struct coord {
int data[ZMAX][YMAX][XMAX];
} coord_t;
__global__ void sample(coord_t * a)
{
int x = threadIdx.x + blockDim.x * blockIdx.x;
int y = threadIdx.y + blockDim.y * blockIdx.y;
int z = threadIdx.z + blockDim.z * blockIdx.z;
a->data[z][y][x] = z * 100 + y * 10 + x;
a->data[z][y][x]++;
}
int main(void)
{
int i, j, k;
int x, y, z;
dim3 BlockPerGrid(1, 1, 1);
dim3 ThreadPerBlock(XMAX, YMAX, ZMAX);
size_t size;
full_t *top;
size = sizeof(full);
cudaMallocManaged(&top, size);
for (i = 0; i < FILE_MAX; i++) {
for (j = 0; j < MOL_MAX; j++) {
for (k = 0; k < ATOM_MAX; k++) {
sample << < BlockPerGrid, ThreadPerBlock >> > ((coord_t *) &(top->array[i][j][k][0][0][0]));
cudaDeviceSynchronize();
}
}
}
for (i = 0; i < FILE_MAX; i++) {
for (j = 0; j < MOL_MAX; j++) {
for (k = 0; k < ATOM_MAX; k++) {
for (z = 0; z < ZMAX; z++) {
for (y = 0; y < YMAX; y++) {
for (x = 0; x < XMAX; x++) {
printf(" %d", top->array[i][j][k][z][y][x]);
}
printf("\n");
}
printf("\n");
}
printf("\n");
}
printf("\n");
}
printf("\n");
printf("\n");
}
cudaFree(top);
}
上一篇: vue项目,webpack中配置src路径别名及使用
下一篇: 并行计算(一)——OpenMP