chundoong-lab-ta/SamsungDS22/submissions/final/c.w.son/B/convolution.cu

418 lines
14 KiB
Plaintext

#include "convolution.h"
#include "util.h"
#include <mpi.h>
#include <stdio.h>
#include <omp.h>
#include <cstdio>
#include <cuda_runtime.h>
#define CUDA_CALL(f) \
{ \
cudaError_t err = (f); \
if (err != cudaSuccess) { \
fprintf(stderr, "CUDA error at [%s:%d] %d %s\n", __FILE__, __LINE__, \
err, cudaGetErrorString(err)); \
exit(1); \
} \
}
static float *input, *output, *filter;
static int N, C, H, W;
static int K, R, S;
static int OH, OW;
static int pad;
static int dilation;
static int stride;
static int mpi_rank, mpi_world_size;
#define MAX_NUM_GPU 4
int num_devices = 0;
int num_devices_change = 4;
// Array of device (GPU) pointers
static float *input_d[MAX_NUM_GPU];
static float *filter_d[MAX_NUM_GPU];
static float *output_d[MAX_NUM_GPU];
static int Nbegin[MAX_NUM_GPU], Nend[MAX_NUM_GPU];
static int nDev = 1;
static int kDev = 1;
static int ohDev = 1;
#define MASTER_TO_SLAVE_TAG 1 //tag for messages sent from master to slaves
#define SLAVE_TO_MASTER_TAG 4 //tag for messages sent from slaves to master
MPI_Request request;
MPI_Status status;
static int min_value(int a, int b) { return a < b ? a : b; }
static int multi_s = 2;
__global__ void sgemm_2(float *input2, float *filter2, float *output2,
int N, int C, int H, int W, int K, int R, int S, int pad, int dilation, int stride,
int dev, int start_n_gpu, int end_n_gpu) {
//int n = blockDim.x * blockIdx.x + threadIdx.x; // raw index
//int k = blockDim.y * blockIdx.y + threadIdx.y; // column index
//int oh = blockDim.z * blockIdx.z + threadIdx.z; // column index
int n = blockIdx.x;
int k = blockIdx.y;
int oh = blockIdx.z;
int ow = threadIdx.x;
if(n >= (end_n_gpu-start_n_gpu)) return;
if(k > K) return;
int OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
int OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
if(oh > OH) return;
if(ow > OW) return;
float o = 0.f;
for (int c = 0; c < C; ++c) {
for (int r = 0; r < R; ++r) {
int h = oh * stride - pad + r * dilation;
for (int s = 0; s < S; ++s) {
//int h = oh * stride - pad + r * dilation;
int w = ow * stride - pad + s * dilation;
if (h < 0 || h >= H || w < 0 || w >= W) continue;
float i = input2[n * C * H * W + c * H * W + h * W + w];
float f = filter2[k * C * R * S + c * R * S + r * S + s];
o += i * f;
//o2 += i2 + f2;
}
}
}
output2[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o;
//printf("Dev(%d)(%d/%d) StartN(%d), EndN(%d), BDx(%d), BDy(%d), Bx(%d), By(%d), Tx(%d), Ty(%d) \n", dev, i, j, start_n_gpu, end_n_gpu ,
// blockDim.x, blockDim.y, blockIdx.x, blockIdx.y, threadIdx.x, threadIdx.y);
}
void convolution(
float *_input, float *_output, float *_filter,
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
input = _input;
output = _output;
filter = _filter;
int size_n;
int start_n;
int end_n;
int start_n_no0;
int end_n_no0;
if(mpi_rank == 0){
for(int node=0; node< mpi_world_size; node++){
size_n = N / (mpi_world_size);
start_n = (node)*size_n;
if(node == 0) start_n_no0 = start_n;
if(((node+1) == mpi_world_size) && ((N%(mpi_world_size)) != 0)){
end_n = N;
if(node == 0) end_n_no0 = end_n;
}
else{
end_n = start_n + size_n;
if(node == 0) end_n_no0 = end_n;
}
if(node >= 1){
MPI_Send(&start_n, 1, MPI_INT, node, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD);
//printf("SEND start_n (%d) to (%d)\n", start_n, node);
MPI_Send(&end_n, 1, MPI_INT, node, MASTER_TO_SLAVE_TAG + 1, MPI_COMM_WORLD);
//printf("SEND end_n (%d) to (%d)\n", end_n, node);
int sizeBuf = (end_n - start_n) * C * H * W;
//printf("SEND input Buff Len : (%d) to (%d)\n", sizeBuf, node);
MPI_Send(&input[start_n*C * H * W], sizeBuf, MPI_FLOAT, node, MASTER_TO_SLAVE_TAG + 2, MPI_COMM_WORLD);
//printf("SEND input size (%d) to (%d)\n", sizeBuf, node);
}
}
}
else{
alloc_tensor(&input, N, C, H, W);
MPI_Recv(&start_n, 1, MPI_INT, 0, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD, &status);
//printf("RECV start_n (%d) at (%d)\n", start_n, mpi_rank);
MPI_Recv(&end_n, 1, MPI_INT, 0, MASTER_TO_SLAVE_TAG + 1, MPI_COMM_WORLD, &status);
//printf("RECV end_n (%d) at (%d)\n", end_n, mpi_rank);
int sizeBuf = (end_n - start_n) * C * H * W;
//printf("RECV input Buff Len : %d \n", sizeBuf);
MPI_Recv(&input[0], sizeBuf, MPI_FLOAT, 0, MASTER_TO_SLAVE_TAG + 2, MPI_COMM_WORLD, &status);
//printf("RECV input size (%d) at (%d)\n", sizeBuf, mpi_rank);
}
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
if(mpi_rank > 0){
alloc_tensor(&filter, K, C, R, S);
}
MPI_Bcast(&filter[0], K*C*R*S, MPI_FLOAT, 0, MPI_COMM_WORLD);
//printf("Bcast filter (%d) to (%d) \n", K*C*R*S, mpi_rank);
if(mpi_rank > 0){
alloc_tensor(&output, N, K, OH, OW);
}
//MPI_Bcast(&output[0], N*K*OH*OW, MPI_FLOAT, 0, MPI_COMM_WORLD);
//printf("Bcast output (%d) to (%d) \n", N*K*OH*OW, mpi_rank);
MPI_Barrier(MPI_COMM_WORLD);
//timer_start(1);
if(mpi_rank == 0){
int size_n_gpu = end_n_no0 - start_n_no0;
// N Devide with GPU
// Setup problem size for each GPU
for (int i = 0; i < num_devices; i++) {
Nbegin[i] = (size_n_gpu / num_devices) * i;
Nend[i] = (size_n_gpu / num_devices) * (i + 1);
}
int mod = (size_n_gpu % num_devices);
int small_size = size_n_gpu / num_devices;
Nend[num_devices - 1] = small_size * num_devices + mod;
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&input_d[i], (Nend[i] - Nbegin[i]) * C * H * W * sizeof(float)) );
CUDA_CALL( cudaMalloc(&filter_d[i], K * C * R * S * sizeof(float)) );
CUDA_CALL( cudaMalloc(&output_d[i], (Nend[i] - Nbegin[i]) * K * OH * OW * sizeof(float)) );
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(input_d[i], input + Nbegin[i] * C * H * W ,
(Nend[i] - Nbegin[i]) * C * H * W * sizeof(float),
cudaMemcpyHostToDevice) );
CUDA_CALL( cudaMemcpy(filter_d[i], filter, K * C * R * S * sizeof(float), cudaMemcpyHostToDevice) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
/**************Start GPU Cal*************************/
/*printf("Start GPU Cal\n");
for (int i = 0; i < num_devices; i++) {
printf("Rank (%d), StartN(%d), EndN(%d)\n", mpi_rank, Nbegin[i], Nend[i]);
} */
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
//dim3 gridDim((Nend[i] - Nbegin[i])*C*H*W, 1, 1); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
//dim3 blockDim(1, 1, 1); // block : 1 x 32
//dim3 gridDim((Nend[i] - Nbegin[i])*C*H*W, 1, 1); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
dim3 gridDim((Nend[i] - Nbegin[i]), K, OH); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
//dim3 blockDim(nDev, kDev, ohDev); // block : 1 x 32
dim3 blockDim(OW, 1);
// 4600 GF
/*
dim3 blockDim(OW, 1);
dim3 gridDim(Mend[i] - Mbegin[i], K, OH);
int n = blockIdx.x;
int k = blockIdx.y;
int oh = blockIdx.z;
int ow = threadIdx.x;
*/
CUDA_CALL( cudaSetDevice(i) );
sgemm_2<<<gridDim, blockDim>>>(input_d[i], filter_d[i], output_d[i], N, C, H, W, K, R, S, pad, dilation, stride, i, Nbegin[i], Nend[i]);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
/* for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
} */
//printf("End GPU Cal\n");
/**************End GPU Cal*************************/
// integrate ouput
// Download C matrix from GPUs
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(output + Nbegin[i] * K * OH * OW , output_d[i],
(Nend[i] - Nbegin[i]) * K * OH * OW * sizeof(float),
cudaMemcpyDeviceToHost) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}
else{
int size_n_gpu = end_n - start_n;
// N Devide with GPU
// Setup problem size for each GPU
for (int i = 0; i < num_devices; i++) {
Nbegin[i] = (size_n_gpu / num_devices) * i;
Nend[i] = (size_n_gpu / num_devices) * (i + 1);
}
int mod = (size_n_gpu % num_devices);
int small_size = size_n_gpu / num_devices;
Nend[num_devices - 1] = small_size * num_devices + mod;
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&input_d[i], (Nend[i] - Nbegin[i]) * C * H * W * sizeof(float)) );
CUDA_CALL( cudaMalloc(&filter_d[i], K * C * R * S * sizeof(float)) );
CUDA_CALL( cudaMalloc(&output_d[i], (Nend[i] - Nbegin[i]) * K * OH * OW * sizeof(float)) );
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(input_d[i], input + Nbegin[i] * C * H * W ,
(Nend[i] - Nbegin[i]) * C * H * W * sizeof(float),
cudaMemcpyHostToDevice) );
CUDA_CALL( cudaMemcpy(filter_d[i], filter, K * C * R * S * sizeof(float), cudaMemcpyHostToDevice) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
/**************Start GPU Cal*************************/
/* printf("Start GPU Cal\n");
for (int i = 0; i < num_devices; i++) {
printf("Rank (%d), StartN(%d), EndN(%d)\n", mpi_rank, Nbegin[i], Nend[i]);
}
*/
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
//dim3 gridDim((Nend[i] - Nbegin[i])*C*H*W, 1, 1); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
//dim3 blockDim(1, 1, 1); // block : 1 x 32
//dim3 gridDim((Nend[i] - Nbegin[i])*C*H*W, 1, 1); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
dim3 gridDim((Nend[i] - Nbegin[i]), K, OH); // grid : raw (Mend[i] - Mbegin[i]) , column (W)
//dim3 blockDim(nDev, kDev, ohDev); // block : 1 x 32
dim3 blockDim(OW, 1);
CUDA_CALL( cudaSetDevice(i) );
sgemm_2<<<gridDim, blockDim>>>(input_d[i], filter_d[i], output_d[i], N, C, H, W, K, R, S, pad, dilation, stride, i, Nbegin[i], Nend[i]);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
/* for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
} */
//printf("End GPU Cal\n");
/**************End GPU Cal*************************/
// integrate ouput
// Download C matrix from GPUs
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(output + Nbegin[i] * K * OH * OW , output_d[i],
(Nend[i] - Nbegin[i]) * K * OH * OW * sizeof(float),
cudaMemcpyDeviceToHost) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
if(mpi_rank >= 1){
MPI_Send(&start_n, 1, MPI_INT, 0, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD);
//printf("SEND output start_n (%d) to (%d)\n", start_n, 0);
MPI_Send(&end_n, 1, MPI_INT, 0, SLAVE_TO_MASTER_TAG + 1, MPI_COMM_WORLD);
//printf("SEND output end_n (%d) to (%d)\n", end_n, 0);
int sizeBuf = (end_n - start_n) * K*OH*OW;
//printf("SEND output Buff Len : (%d) to (%d)\n", sizeBuf, 0);
MPI_Send(&output[0], sizeBuf, MPI_FLOAT, 0, SLAVE_TO_MASTER_TAG + 2, MPI_COMM_WORLD);
//printf("SEND output size (%d) to (%d)\n", sizeBuf, 0);
}
}
//double elapsed_time = timer_stop(1);
//double flops = (double) 2.0 * N * OH * OW * K * C * R * S / elapsed_time;
//printf("Rank (%d), %f sec\n", mpi_rank, elapsed_time);
if(mpi_rank == 0){
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
for (int node = 1; node < mpi_world_size; node++) {// untill all slaves have handed back the processed data
//receive low bound from a slave
MPI_Recv(&start_n, 1, MPI_INT, node, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD, &status);
//receive upper bound from a slave
MPI_Recv(&end_n, 1, MPI_INT, node, SLAVE_TO_MASTER_TAG + 1, MPI_COMM_WORLD, &status);
//receive processed data from a slave
MPI_Recv(&output[start_n*K*OH*OW], (end_n - start_n) * K*OH*OW, MPI_FLOAT, node, SLAVE_TO_MASTER_TAG + 2, MPI_COMM_WORLD, &status);
}
}
}
void convolution_init(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
N = _N; C = _C; H = _H; W = _W;
K = _K; R = _R; S = _S;
pad = _pad;
dilation = _dilation;
stride = _stride;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size);
// cuda call init
CUDA_CALL( cudaGetDeviceCount(&num_devices) );
printf("Using %d devices\n", num_devices);
printf("Change Device Num (%d)->(%d)\n", num_devices, num_devices_change);
num_devices = num_devices_change;
for (int i = 0; i < num_devices; i++) {
cudaDeviceProp prop;
CUDA_CALL( cudaGetDeviceProperties(&prop, i) );
// Try printing more detailed information here
printf("[GPU %d] %s\n", i, prop.name);
}
if (num_devices <= 0) {
printf("No CUDA device found. Aborting\n");
exit(1);
}
}
void convolution_final(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
}