hip_filename
stringlengths 5
84
| hip_content
stringlengths 79
9.69M
| cuda_filename
stringlengths 4
83
| cuda_content
stringlengths 19
9.69M
|
---|---|---|---|
fcd394853732933cc2ddcf59fa29d561f0263cb1.hip | // !!! This is a file automatically generated by hipify!!!
#include "hip/hip_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iso646.h>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include "SyncedMemory.h"
#include "lab1.h"
using namespace std;
#define CHECK {\
auto e = hipDeviceSynchronize();\
if (e != hipSuccess) {\
printf("At " __FILE__ ":%d, %s\n", __LINE__, hipGetErrorString(e));\
abort();\
}\
}
int main(int argc, char **argv)
{
Lab1VideoGenerator g;
Lab1VideoInfo i;
g.get_info(i);
if (i.w == 0 | i.h == 0 | i.n_frame == 0 | i.fps_n == 0 | i.fps_d == 0) {
puts("Cannot be zero");
abort();
} else if (i.w%2 != 0 | i.h%2 != 0) {
puts("Only even frame size is supported");
abort();
}
unsigned FRAME_SIZE = i.w*i.h*3/2;
MemoryBuffer<uint8_t> frameb(FRAME_SIZE);
auto frames = frameb.CreateSync(FRAME_SIZE);
FILE *fp = fopen("result.y4m", "wb");
printf("start");
fprintf(fp, "YUV4MPEG2 W%d H%d F%d:%d Ip A1:1 C420\n", i.w, i.h, i.fps_n, i.fps_d);
for (unsigned j = 0; j < i.n_frame; ++j) {
fputs("FRAME\n", fp);
g.Generate(frames.get_gpu_wo());
fwrite(frames.get_cpu_ro(), sizeof(uint8_t), FRAME_SIZE, fp);
}
fclose(fp);
printf("end");
return 0;
} | fcd394853732933cc2ddcf59fa29d561f0263cb1.cu |
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iso646.h>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include "SyncedMemory.h"
#include "lab1.h"
using namespace std;
#define CHECK {\
auto e = cudaDeviceSynchronize();\
if (e != cudaSuccess) {\
printf("At " __FILE__ ":%d, %s\n", __LINE__, cudaGetErrorString(e));\
abort();\
}\
}
int main(int argc, char **argv)
{
Lab1VideoGenerator g;
Lab1VideoInfo i;
g.get_info(i);
if (i.w == 0 | i.h == 0 | i.n_frame == 0 | i.fps_n == 0 | i.fps_d == 0) {
puts("Cannot be zero");
abort();
} else if (i.w%2 != 0 | i.h%2 != 0) {
puts("Only even frame size is supported");
abort();
}
unsigned FRAME_SIZE = i.w*i.h*3/2;
MemoryBuffer<uint8_t> frameb(FRAME_SIZE);
auto frames = frameb.CreateSync(FRAME_SIZE);
FILE *fp = fopen("result.y4m", "wb");
printf("start");
fprintf(fp, "YUV4MPEG2 W%d H%d F%d:%d Ip A1:1 C420\n", i.w, i.h, i.fps_n, i.fps_d);
for (unsigned j = 0; j < i.n_frame; ++j) {
fputs("FRAME\n", fp);
g.Generate(frames.get_gpu_wo());
fwrite(frames.get_cpu_ro(), sizeof(uint8_t), FRAME_SIZE, fp);
}
fclose(fp);
printf("end");
return 0;
} |
d654bdeca448d1a413a7cc87ccc3b4b7f18a965d.hip | // !!! This is a file automatically generated by hipify!!!
/*
* Discrete Cosine/Sine Transform(DCT/DST and IDCT/IDST one to four-all in one)
* DCT/DST and IDCT/IDST I ---> IV
* This CUDA code can handle/work with any type of the input mxArrays,
* GPUarray or standard matlab CPU array as input {prhs[0] := mxGPUArray or CPU Array}
* GpuArray/cpuArray output, B=Discrete_Transform(A, , type of Transform (sine or cosine), type of Transform(direct/inverse), type of DCT/DST or IDCT/IDST, dimensions).
* Developed at UCL, Institute of Neurology, 12 Queen Square, WC1N 3AR, London
* Wellcome Trust Centre for Neuroimaging
* Part of the project SPM(http://www.fil.ion.ucl.ac.uk/spm)
* Copyright 2018
* Kevin Bronik
*/
#include "matrix.h"
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include "CuFilesD/Discrete_Transform_kernel.cuh"
#include "CuFilesD/DCT_I_Column.cu"
#include "CuFilesD/DCT_I_Row.cu"
#include "CuFilesD/DCT_I_Column_Inverse.cu"
#include "CuFilesD/DCT_I_Row_Inverse.cu"
#include "CuFilesD/DCT_II_Row.cu"
#include "CuFilesD/DCT_II_Row_Inverse.cu"
#include "CuFilesD/DCT_II_Column.cu"
#include "CuFilesD/DCT_II_Column_Inverse.cu"
#include "CuFilesD/DCT_III_Row.cu"
#include "CuFilesD/DCT_III_Row_Inverse.cu"
#include "CuFilesD/DCT_III_Column.cu"
#include "CuFilesD/DCT_III_Column_Inverse.cu"
#include "CuFilesD/DCT_IV_Row.cu"
#include "CuFilesD/DCT_IV_Row_Inverse.cu"
#include "CuFilesD/DCT_IV_Column.cu"
#include "CuFilesD/DCT_IV_Column_Inverse.cu"
#include "CuFilesD/DST_I_Column.cu"
#include "CuFilesD/DST_I_Row.cu"
#include "CuFilesD/DST_I_Column_Inverse.cu"
#include "CuFilesD/DST_I_Row_Inverse.cu"
#include "CuFilesD/DST_II_Row.cu"
#include "CuFilesD/DST_II_Row_Inverse.cu"
#include "CuFilesD/DST_II_Column.cu"
#include "CuFilesD/DST_II_Column_Inverse.cu"
#include "CuFilesD/DST_III_Row.cu"
#include "CuFilesD/DST_III_Row_Inverse.cu"
#include "CuFilesD/DST_III_Column.cu"
#include "CuFilesD/DST_III_Column_Inverse.cu"
#include "CuFilesD/DST_IV_Row.cu"
#include "CuFilesD/DST_IV_Row_Inverse.cu"
#include "CuFilesD/DST_IV_Column.cu"
#include "CuFilesD/DST_IV_Column_Inverse.cu"
//#include <math.h>
#include <hip/hip_runtime.h>
#include <hip/hip_runtime.h>
#define DEFAULT_DIM 32
#define DELTA(i, j) ((i==j)?1:0)
//#define TILE_DIM 16
unsigned int TILE_DIM=16;
// DCT
extern "C" void CalculateTransformDCTColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
// DST
extern "C" void CalculateTransformDSTColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" static void mexTransD(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
int nDevices;
hipError_t errCode =hipGetDeviceCount(&nDevices);
//int nDevices;
//hipGetDeviceCount(&nDevices);
if (errCode != hipSuccess){
printf("Error! No CUDA devices found! \n");
return;
}
char row[] = "row";
char column[] = "column";
char one[] = "one";
char two[] = "two";
char three[] = "three";
char four[] = "four";
char direct[] = "direct";
char inverse[] = "inverse";
char cosine[] = "cosine";
char sine[] = "sine";
char const * const InputErrMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float), and the number of input arguments must be five.";
if ((nrhs!=5)) {
mexErrMsgIdAndTxt("MATLAB:mexatexit:invalidInput", InputErrMsg);
}
char *input_buf0;
input_buf0 = mxArrayToString(prhs[0]);
char *input_buf1;
input_buf1 = mxArrayToString(prhs[1]);
char *input_buf2;
input_buf2 = mxArrayToString(prhs[2]);
char *input_buf3;
input_buf3 = mxArrayToString(prhs[3]);
char *input_buf4;
input_buf4 = mxArrayToString(prhs[4]);
if ((mxIsChar(prhs[0]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FIRST ARGUMENT) must be array, or gpuArray object not %s\n",input_buf0);
}
if (!(mxIsChar(prhs[1]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(SECOND ARGUMENT) must be of type string.\n.");
}
if (!(mxIsChar(prhs[2]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(THIRD ARGUMENT) must be of type string.\n.");
}
if (!(mxIsChar(prhs[3]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FOURTH ARGUMENT) must be of type string.\n.");
}
if (!(mxIsChar(prhs[4]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FIFTH ARGUMENT) must be of type string.\n.");
}
if ((strcmp (cosine,input_buf1) != 0) &&(strcmp (sine,input_buf1) != 0) )
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(SECOND ARGUMENT) must be 'cosine' or 'sine' not %s\n",input_buf1);
}
if ((strcmp (direct,input_buf2) != 0)&& (strcmp (inverse,input_buf2) != 0) )
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(THIRD ARGUMENT) must be 'direct' or 'inverse' not %s\n",input_buf2);
}
if ((strcmp (one,input_buf3) != 0)&& (strcmp (two,input_buf3) != 0) && (strcmp (three,input_buf3) != 0) && (strcmp (four,input_buf3) != 0))
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FOURTH ARGUMENT) must be 'one' or 'two' or 'three' or 'four' not %s\n",input_buf3);
}
if ((strcmp (column,input_buf4) != 0)&&(strcmp (row,input_buf4) != 0))
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FIFTH ARGUMENT) must be 'column' or 'row' not %s\n",input_buf4);
}
//COSINE TRANSFORM
if (strcmp (cosine,input_buf1) == 0)
{
if (strcmp (direct,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Cosine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Cosine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer =(float*) mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Cosine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Cosine Transform in column wise \n");
return;
}
mxInitGPU();
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
if (strcmp (inverse,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Cosine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Cosine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Cosine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Cosine Transform in column wise \n");
return;
}
mxInitGPU();
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer =(float*) mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTInverseRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTInverseRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTInverseRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTInverseRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
}
//SINE TRANSFORM
if (strcmp (sine,input_buf1) == 0)
{
if (strcmp (direct,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Sine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Sine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Sine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Sine Transform in column wise \n");
return;
}
mxInitGPU();
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
if (strcmp (inverse,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Sine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Sine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
hipError_t error;
int devID = 0;
error = hipGetDevice(&devID);
hipDeviceProp_t deviceProp;
error = hipGetDeviceProperties(&deviceProp, devID);
if (error != hipSuccess)
{
printf("hipGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Sine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Sine Transform in column wise \n");
return;
}
mxInitGPU();
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer =(float*) mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTInverseRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTInverseRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTInverseRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTInverseRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
}
}
| d654bdeca448d1a413a7cc87ccc3b4b7f18a965d.cu |
/*
* Discrete Cosine/Sine Transform(DCT/DST and IDCT/IDST one to four-all in one)
* DCT/DST and IDCT/IDST I ---> IV
* This CUDA code can handle/work with any type of the input mxArrays,
* GPUarray or standard matlab CPU array as input {prhs[0] := mxGPUArray or CPU Array}
* GpuArray/cpuArray output, B=Discrete_Transform(A, , type of Transform (sine or cosine), type of Transform(direct/inverse), type of DCT/DST or IDCT/IDST, dimensions).
* Developed at UCL, Institute of Neurology, 12 Queen Square, WC1N 3AR, London
* Wellcome Trust Centre for Neuroimaging
* Part of the project SPM(http://www.fil.ion.ucl.ac.uk/spm)
* Copyright 2018
* Kevin Bronik
*/
#include "matrix.h"
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include "CuFilesD/Discrete_Transform_kernel.cuh"
#include "CuFilesD/DCT_I_Column.cu"
#include "CuFilesD/DCT_I_Row.cu"
#include "CuFilesD/DCT_I_Column_Inverse.cu"
#include "CuFilesD/DCT_I_Row_Inverse.cu"
#include "CuFilesD/DCT_II_Row.cu"
#include "CuFilesD/DCT_II_Row_Inverse.cu"
#include "CuFilesD/DCT_II_Column.cu"
#include "CuFilesD/DCT_II_Column_Inverse.cu"
#include "CuFilesD/DCT_III_Row.cu"
#include "CuFilesD/DCT_III_Row_Inverse.cu"
#include "CuFilesD/DCT_III_Column.cu"
#include "CuFilesD/DCT_III_Column_Inverse.cu"
#include "CuFilesD/DCT_IV_Row.cu"
#include "CuFilesD/DCT_IV_Row_Inverse.cu"
#include "CuFilesD/DCT_IV_Column.cu"
#include "CuFilesD/DCT_IV_Column_Inverse.cu"
#include "CuFilesD/DST_I_Column.cu"
#include "CuFilesD/DST_I_Row.cu"
#include "CuFilesD/DST_I_Column_Inverse.cu"
#include "CuFilesD/DST_I_Row_Inverse.cu"
#include "CuFilesD/DST_II_Row.cu"
#include "CuFilesD/DST_II_Row_Inverse.cu"
#include "CuFilesD/DST_II_Column.cu"
#include "CuFilesD/DST_II_Column_Inverse.cu"
#include "CuFilesD/DST_III_Row.cu"
#include "CuFilesD/DST_III_Row_Inverse.cu"
#include "CuFilesD/DST_III_Column.cu"
#include "CuFilesD/DST_III_Column_Inverse.cu"
#include "CuFilesD/DST_IV_Row.cu"
#include "CuFilesD/DST_IV_Row_Inverse.cu"
#include "CuFilesD/DST_IV_Column.cu"
#include "CuFilesD/DST_IV_Column_Inverse.cu"
//#include <math.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define DEFAULT_DIM 32
#define DELTA(i, j) ((i==j)?1:0)
//#define TILE_DIM 16
unsigned int TILE_DIM=16;
// DCT
extern "C" void CalculateTransformDCTColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDCTInverseRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
// DST
extern "C" void CalculateTransformDSTColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowOne(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnTwo(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowThree(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseColumnFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" void CalculateTransformDSTInverseRowFour(float * A, float * C, int numARows,
int numAColumns, int numCRows, int numCColumns);
extern "C" static void mexTransD(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
int nDevices;
cudaError_t errCode =cudaGetDeviceCount(&nDevices);
//int nDevices;
//cudaGetDeviceCount(&nDevices);
if (errCode != cudaSuccess){
printf("Error! No CUDA devices found! \n");
return;
}
char row[] = "row";
char column[] = "column";
char one[] = "one";
char two[] = "two";
char three[] = "three";
char four[] = "four";
char direct[] = "direct";
char inverse[] = "inverse";
char cosine[] = "cosine";
char sine[] = "sine";
char const * const InputErrMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float), and the number of input arguments must be five.";
if ((nrhs!=5)) {
mexErrMsgIdAndTxt("MATLAB:mexatexit:invalidInput", InputErrMsg);
}
char *input_buf0;
input_buf0 = mxArrayToString(prhs[0]);
char *input_buf1;
input_buf1 = mxArrayToString(prhs[1]);
char *input_buf2;
input_buf2 = mxArrayToString(prhs[2]);
char *input_buf3;
input_buf3 = mxArrayToString(prhs[3]);
char *input_buf4;
input_buf4 = mxArrayToString(prhs[4]);
if ((mxIsChar(prhs[0]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FIRST ARGUMENT) must be array, or gpuArray object not %s\n",input_buf0);
}
if (!(mxIsChar(prhs[1]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(SECOND ARGUMENT) must be of type string.\n.");
}
if (!(mxIsChar(prhs[2]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(THIRD ARGUMENT) must be of type string.\n.");
}
if (!(mxIsChar(prhs[3]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FOURTH ARGUMENT) must be of type string.\n.");
}
if (!(mxIsChar(prhs[4]))){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FIFTH ARGUMENT) must be of type string.\n.");
}
if ((strcmp (cosine,input_buf1) != 0) &&(strcmp (sine,input_buf1) != 0) )
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(SECOND ARGUMENT) must be 'cosine' or 'sine' not %s\n",input_buf1);
}
if ((strcmp (direct,input_buf2) != 0)&& (strcmp (inverse,input_buf2) != 0) )
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(THIRD ARGUMENT) must be 'direct' or 'inverse' not %s\n",input_buf2);
}
if ((strcmp (one,input_buf3) != 0)&& (strcmp (two,input_buf3) != 0) && (strcmp (three,input_buf3) != 0) && (strcmp (four,input_buf3) != 0))
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FOURTH ARGUMENT) must be 'one' or 'two' or 'three' or 'four' not %s\n",input_buf3);
}
if ((strcmp (column,input_buf4) != 0)&&(strcmp (row,input_buf4) != 0))
{
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Input(FIFTH ARGUMENT) must be 'column' or 'row' not %s\n",input_buf4);
}
//COSINE TRANSFORM
if (strcmp (cosine,input_buf1) == 0)
{
if (strcmp (direct,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Cosine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Cosine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer =(float*) mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Cosine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Cosine Transform in column wise \n");
return;
}
mxInitGPU();
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
if (strcmp (inverse,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Cosine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Cosine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTInverseColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Cosine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DCTI_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DCTII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DCTIII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DCTIV_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Cosine Transform in column wise \n");
return;
}
mxInitGPU();
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer =(float*) mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDCTInverseRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDCTInverseRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDCTInverseRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDCTInverseRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
}
//SINE TRANSFORM
if (strcmp (sine,input_buf1) == 0)
{
if (strcmp (direct,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Sine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Discrete Sine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Sine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Discrete Sine Transform in column wise \n");
return;
}
mxInitGPU();
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
if (strcmp (inverse,input_buf2) == 0)
{
if (strcmp (column,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Sine Transform in row wise \n");
return;
}
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Inverse_Kernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Column_Inverse_Kernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
numCRows = numARows;
numCColumns = numAColumns;
if (numARows==1)
{
printf("Attention, this is a row vector, please try Inverse Discrete Sine Transform in row wise \n");
return;
}
mxInitGPU();
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer = (float*)mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTInverseColumnFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
if (strcmp (row,input_buf4) == 0)
{
if (mxIsGPUArray(prhs[0])) {
mxGPUArray const *A;
mxGPUArray *B;
float const *d_A;
float *d_B;
int numARows, numAColumns, numCRows, numCColumns;
mxInitGPU();
cudaError_t error;
int devID = 0;
error = cudaGetDevice(&devID);
cudaDeviceProp deviceProp;
error = cudaGetDeviceProperties(&deviceProp, devID);
if (error != cudaSuccess)
{
printf("cudaGetDeviceProperties returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
int TILEDIM = (deviceProp.major < 2) ? 16 : 32;
A = mxGPUCreateFromMxArray(prhs[0]);
if(mxGPUGetComplexity(A) != mxREAL){
mxGPUDestroyGPUArray(A);
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
const mwSize *dims;
dims=mxGPUGetDimensions(A);
numARows = (int)dims[0]; /* gets number of rows of A */
numAColumns = (int)dims[1]; /* gets number of columns of A */
size_t pivot_dimensA[2] = {numARows,numAColumns};
mwSize NrOfDim=mxGPUGetNumberOfDimensions(A);
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Sine Transform in column wise \n");
return;
}
numCRows = numARows;
numCColumns = numAColumns;
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
char const * const errMsg = "Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).";
if (mxGPUGetClassID(A) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt(errId, errMsg);
}
d_A = (float const *)(mxGPUGetDataReadOnly(A));
mxGPUDestroyGPUArray(A);
B = mxGPUCreateGPUArray(NrOfDim, (mwSize*) pivot_dimensA, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
d_B = (float *)(mxGPUGetData(B));
dim3 dimBlock;
dim3 dimGrid;
switch (TILEDIM){
case 16:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row__InverseKernel_GPUA <16> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
case 32:
TILE_DIM= TILEDIM;
dimBlock.x=TILE_DIM;
dimBlock.y=TILE_DIM;
dimBlock.z=1;
dimGrid.x = (numCColumns + dimBlock.x - 1) / dimBlock.x;
dimGrid.y = (numCRows + dimBlock.y - 1) / dimBlock.y;
if (strcmp (one,input_buf3) == 0)
{
DSTI_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
DSTII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
DSTIII_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
DSTIV_Row__InverseKernel_GPUA <32> << <dimGrid, dimBlock >> >(d_A, d_B, numARows, numAColumns, numCRows, numCColumns);
}
plhs[0] = mxGPUCreateMxArrayOnGPU(B);
mxGPUDestroyGPUArray(B);
}
}
else if (!(mxIsGPUArray(prhs[0]))){
if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS) {
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Invalid input to MEX file, input(FIRST ARGUMENT) must be single precision (float).");
}
if(mxIsComplex(prhs[0])){
mexErrMsgIdAndTxt( "MATLAB:mexatexit:invalidInput",
"Incorrect input arguments!, input matrix must be real %s\n");
}
int numARows = (int)mxGetM(prhs[0]); // number of rows in the matrix A
int numAColumns = (int)mxGetN(prhs[0]); // number of columns in the matrix A
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)
if (numAColumns==1)
{
printf("Attention, this is a column vector, please try Inverse Discrete Sine Transform in column wise \n");
return;
}
mxInitGPU();
numCRows = numARows;
numCColumns = numAColumns;
float * hostA ; // The A matrix
hostA = (float *)mxGetData(prhs[0]);
plhs[0] = mxCreateNumericMatrix(numCRows, numCColumns, mxSINGLE_CLASS, mxREAL);
float *pointer =(float*) mxGetPr(plhs[0]);
if (strcmp (one,input_buf3) == 0)
{
CalculateTransformDSTInverseRowOne(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (two,input_buf3) == 0)
{
CalculateTransformDSTInverseRowTwo(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (three,input_buf3) == 0)
{
CalculateTransformDSTInverseRowThree(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
if (strcmp (four,input_buf3) == 0)
{
CalculateTransformDSTInverseRowFour(hostA, pointer, numARows, numAColumns, numCRows, numCColumns);
}
}
}
}
}
}
|
464e3d1584f0013dfda51116d9aaaf21bd91bc13.hip | // !!! This is a file automatically generated by hipify!!!
#include "lab08.cuh"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cstdio>
#include "MPI_dummy_helper.hpp"
#include "dummy_helper.cuh"
#include <mpi.h>
#include <hip/hip_runtime.h>
#include <thrust/device_vector.h>
#include <thrust/extrema.h>
#define GRID_SIZE 1
#define BLOCK_SIZE 4
#define GRID_SIZE_dim3 dim3(GRID_SIZE, GRID_SIZE, GRID_SIZE)
#define BLOCK_SIZE_dim3 dim3(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
#define locate(i, j, k) block_h[(i) + (j) * block_shape[0] + (k) * block_shape[0] * block_shape[1]]
#define locate_p(v, i, j, k) v[(i) + (j) * block_shape[0] + (k) * block_shape[0] * block_shape[1]]
__global__
void init_array(
double *v,
long long count,
double init_value
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long id = idx + idy * blockDim.x * gridDim.x + idz * blockDim.x * gridDim.x * blockDim.y * gridDim.y;
const long long offset = gridDim.x * blockDim.x * gridDim.y * blockDim.y * gridDim.z * blockDim.z;
for (long long i = id; i < count; i += offset)
v[i] = init_value;
}
void Lab08::set_device()
{
int device_count;
checkCudaErrors(hipGetDeviceCount(&device_count));
checkCudaErrors(hipSetDevice(rank % device_count));
}
Lab08::Lab08(int argc, char **argv)
{
init(argc, argv);
checkMPIErrors(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
set_device();
// read input data
if (rank == 0)
rank_0_init();
else
rank_non_0_init();
block_z = rank / process_grid_shape[0] / process_grid_shape[1];
block_y = rank % (process_grid_shape[0] * process_grid_shape[1]) / process_grid_shape[0];
block_x = rank % (process_grid_shape[0] * process_grid_shape[1]) % process_grid_shape[0];
sends_first = (block_x + block_y + block_z) % 2;
CudaKernelChecker kernel_checker;
auto block_init_routine =
[&kernel_checker, this](CudaMemory<double> &v, const char* kernel_name)
{
v.alloc(block_shape[0] * block_shape[1] * block_shape[2]);
hipLaunchKernelGGL(( init_array), dim3(GRID_SIZE_dim3), dim3(BLOCK_SIZE_dim3), 0, 0,
v.get(),
block_shape[0] * block_shape[1] * block_shape[2],
u_0
);
kernel_checker.check(kernel_name);
};
block_init_routine(block_d, "init block_d");
block_init_routine(prev_block_d, "init prev_block_d");
auto boundary_layer_init_routine =
[this](
std::vector<double> &v_h,
CudaMemory<double> &v_d,
const bool layer_not_needed,
const long long count
)
{
v_h.resize( layer_not_needed ? 0 : count);
v_d.alloc( layer_not_needed ? 0 : count);
};
boundary_layer_init_routine(
left_h,
left_d,
block_x == 0,
block_shape[1] * block_shape[2]
);
boundary_layer_init_routine(
right_h,
right_d,
block_x == process_grid_shape[0] - 1,
block_shape[1] * block_shape[2]
);
boundary_layer_init_routine(
front_h,
front_d,
block_y == 0,
block_shape[0] * block_shape[2]
);
boundary_layer_init_routine(
back_h,
back_d,
block_y == process_grid_shape[1] - 1,
block_shape[0] * block_shape[2]
);
boundary_layer_init_routine(
down_h,
down_d,
block_z == 0,
block_shape[0] * block_shape[1]
);
boundary_layer_init_routine(
up_h,
up_d,
block_z == process_grid_shape[2] - 1,
block_shape[0] * block_shape[1]
);
timer.start();
}
void Lab08::init(int argc, char **argv)
{
int initialized;
checkMPIErrors(MPI_Initialized(
&initialized
));
if (!initialized)
{
checkMPIErrors(MPI_Init(&argc, &argv));
}
}
void Lab08::finalize()
{
int finalized;
checkMPIErrors(MPI_Finalized(
&finalized
));
if (!finalized)
{
checkMPIErrors(MPI_Barrier(MPI_COMM_WORLD));
checkMPIErrors(MPI_Finalize());
}
}
void Lab08::rank_0_init()
{
// input
read_in_container(process_grid_shape);
read_in_container(block_shape);
std::cin >> output_name;
std::cin >> eps;
read_in_container(l);
std::cin >> boundaries.down
>> boundaries.up
>> boundaries.left
>> boundaries.right
>> boundaries.front
>> boundaries.back;
std::cin >> u_0;
// input done
// send input data to other ranks
int n_ranks = process_grid_shape[0]
* process_grid_shape[1]
* process_grid_shape[2];
for (int rank = 1; rank < n_ranks; ++rank)
{
checkMPIErrors(MPI_Send(
process_grid_shape.data(),
process_grid_shape.size(),
MPI_LONG_LONG,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
block_shape.data(),
block_shape.size(),
MPI_LONG_LONG,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
output_name.data(),
output_name.size(),
MPI_CHAR,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
&eps,
1,
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
l.data(),
l.size(),
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
reinterpret_cast<double*>(&boundaries),
sizeof(boundaries) / sizeof(double),
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
&u_0,
1,
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
}
}
void Lab08::rank_non_0_init()
{
int root_rank = 0;
checkMPIErrors(MPI_Recv(
process_grid_shape.data(),
process_grid_shape.size(),
MPI_LONG_LONG,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
block_shape.data(),
block_shape.size(),
MPI_LONG_LONG,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
MPI_Status status;
checkMPIErrors(MPI_Probe(
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
&status
));
int output_name_count;
checkMPIErrors(MPI_Get_count(
&status,
MPI_CHAR,
&output_name_count
));
std::vector<char> output_name_buffer(output_name_count);
checkMPIErrors(MPI_Recv(
output_name_buffer.data(),
output_name_buffer.size(),
MPI_CHAR,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
std::copy(
output_name_buffer.begin(),
output_name_buffer.end(),
std::back_inserter(output_name)
);
checkMPIErrors(MPI_Recv(
&eps,
1,
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
l.data(),
l.size(),
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
reinterpret_cast<double*>(&boundaries),
sizeof(boundaries) / sizeof(double),
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
&u_0,
1,
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
}
int Lab08::block_position_to_rank(
long long block_x,
long long block_y,
long long block_z
)
{
if (block_x < 0 || block_y < 0 || block_z < 0)
{
return -1;
}
if (
block_x >= process_grid_shape[0]
|| block_y >= process_grid_shape[1]
|| block_z >= process_grid_shape[2]
)
{
return -1;
}
return block_x
+ block_y * process_grid_shape[0]
+ block_z * process_grid_shape[0] * process_grid_shape[1];
}
void Lab08::send_boundary_layer(
std::vector<double> &v_h,
CudaMemory<double> &v_d,
int destination_rank,
Lab08::BoundaryLayerTag tag,
std::vector<MPI_Request> &send_requests
)
{
if (destination_rank > -1)
{
v_d.memcpy(
v_h.data(),
hipMemcpyDeviceToHost
);
MPI_Request request;
checkMPIErrors(MPI_Isend(
v_h.data(),
v_h.size(),
MPI_DOUBLE,
destination_rank,
static_cast<int>(tag),
MPI_COMM_WORLD,
&request
));
send_requests.push_back(request);
}
}
void Lab08::send_boundary_layers(std::vector<MPI_Request> &send_requests)
{
send_boundary_layer(
left_h,
left_d,
block_position_to_rank(block_x - 1, block_y, block_z),
BoundaryLayerTag::RIGHT,
send_requests
);
send_boundary_layer(
right_h,
right_d,
block_position_to_rank(block_x + 1, block_y, block_z),
BoundaryLayerTag::LEFT,
send_requests
);
send_boundary_layer(
front_h,
front_d,
block_position_to_rank(block_x, block_y - 1, block_z),
BoundaryLayerTag::BACK,
send_requests
);
send_boundary_layer(
back_h,
back_d,
block_position_to_rank(block_x, block_y + 1, block_z),
BoundaryLayerTag::FRONT,
send_requests
);
send_boundary_layer(
down_h,
down_d,
block_position_to_rank(block_x, block_y, block_z - 1),
BoundaryLayerTag::UP,
send_requests
);
send_boundary_layer(
up_h,
up_d,
block_position_to_rank(block_x, block_y, block_z + 1),
BoundaryLayerTag::DOWN,
send_requests
);
}
void Lab08::receive_boundary_layer(
std::vector<double> &v,
int source_rank,
BoundaryLayerTag tag,
std::vector<MPI_Request> &receive_requests
)
{
if (source_rank > -1)
{
MPI_Request request;
checkMPIErrors(MPI_Irecv(
v.data(),
v.size(),
MPI_DOUBLE,
source_rank,
static_cast<int>(tag),
MPI_COMM_WORLD,
&request
));
receive_requests.push_back(request);
}
}
void Lab08::receive_boundary_layers(
std::vector<double> &left_h,
std::vector<double> &right_h,
std::vector<double> &front_h,
std::vector<double> &back_h,
std::vector<double> &down_h,
std::vector<double> &up_h,
std::vector<MPI_Request> &receive_requests
)
{
receive_boundary_layer(
left_h,
block_position_to_rank(block_x - 1, block_y, block_z),
BoundaryLayerTag::LEFT,
receive_requests
);
receive_boundary_layer(
right_h,
block_position_to_rank(block_x + 1, block_y, block_z),
BoundaryLayerTag::RIGHT,
receive_requests
);
receive_boundary_layer(
front_h,
block_position_to_rank(block_x, block_y - 1, block_z),
BoundaryLayerTag::FRONT,
receive_requests
);
receive_boundary_layer(
back_h,
block_position_to_rank(block_x, block_y + 1, block_z),
BoundaryLayerTag::BACK,
receive_requests
);
receive_boundary_layer(
down_h,
block_position_to_rank(block_x, block_y, block_z - 1),
BoundaryLayerTag::DOWN,
receive_requests
);
receive_boundary_layer(
up_h,
block_position_to_rank(block_x, block_y, block_z + 1),
BoundaryLayerTag::UP,
receive_requests
);
}
__device__ long long block_shape[3];
__global__
void copy_block_to_prev_block(
double *block,
double *prev_block
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
for (long long k = idz; k < block_shape[2]; k += offset_z)
for (long long j = idy; j < block_shape[1]; j += offset_y)
for (long long i = idx; i < block_shape[0]; i += offset_x)
locate_p(prev_block, i, j, k) = locate_p(block, i, j, k);
}
__global__
void prev_block_to_abs_difference_array(
double *block,
double *prev_block
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
for (long long k = idz; k < block_shape[2]; k += offset_z)
for (long long j = idy; j < block_shape[1]; j += offset_y)
for (long long i = idx; i < block_shape[0]; i += offset_x)
locate_p(prev_block, i, j, k) = fabs(locate_p(block, i, j, k) - locate_p(prev_block, i, j, k));
}
__global__
void block_iter_process(
double *block,
double *prev_block,
double *left,
double *right,
double *front,
double *back,
double *down,
double *up,
Lab08::Boundaries boundaries,
double h_x_pow_minus_2,
double h_y_pow_minus_2,
double h_z_pow_minus_2,
double denominator
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
for (long long k = idz; k < block_shape[2]; k += offset_z)
for (long long j = idy; j < block_shape[1]; j += offset_y)
for (long long i = idx; i < block_shape[0]; i += offset_x)
{
double u_left = i == 0 ? (left == nullptr ? boundaries.left : left[ j + block_shape[1] * k]) : locate_p(prev_block, i - 1, j, k),
u_right = i == block_shape[0] - 1 ? (right == nullptr ? boundaries.right : right[j + block_shape[1] * k]) : locate_p(prev_block, i + 1, j, k),
u_front = j == 0 ? (front == nullptr ? boundaries.front : front[i + block_shape[0] * k]) : locate_p(prev_block, i, j - 1, k),
u_back = j == block_shape[1] - 1 ? (back == nullptr ? boundaries.back : back[ i + block_shape[0] * k]) : locate_p(prev_block, i, j + 1, k),
u_down = k == 0 ? (down == nullptr ? boundaries.down : down[ i + block_shape[0] * j]) : locate_p(prev_block, i, j, k - 1),
u_up = k == block_shape[2] - 1 ? (up == nullptr ? boundaries.up : up[ i + block_shape[0] * j]) : locate_p(prev_block, i, j, k + 1);
locate_p(block, i, j, k) = (u_left + u_right) * h_x_pow_minus_2;
locate_p(block, i, j, k) += (u_front + u_back ) * h_y_pow_minus_2;
locate_p(block, i, j, k) += (u_down + u_up ) * h_z_pow_minus_2;
locate_p(block, i, j, k) /= denominator;
}
}
__global__
void init_boundary_layers(
double *block,
double *left,
double *right,
double *front,
double *back,
double *down,
double *up
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
#define fill_boundary_layer(v, outer, inner, outer_start, inner_start, outer_offset, inner_offset, loc) \
{ \
if (v) \
{ \
for (long long i = outer_start; i < block_shape[outer]; i += outer_offset) \
for (long long j = inner_start; j < block_shape[inner]; j += inner_offset) \
v[i * block_shape[inner] + j] = loc; \
} \
} \
if (idx == 0)
fill_boundary_layer(left , 2, 1, idz, idy, offset_z, offset_y, locate_p(block, 0, j, i));
if (idx == 1)
fill_boundary_layer(right, 2, 1, idz, idy, offset_z, offset_y, locate_p(block, block_shape[0] - 1, j, i));
if (idy == 0)
fill_boundary_layer(front, 2, 0, idz, idx, offset_z, offset_x, locate_p(block, j, 0, i));
if (idy == 1)
fill_boundary_layer(back , 2, 0, idz, idx, offset_z, offset_x, locate_p(block, j, block_shape[1] - 1, i));
if (idz == 0)
fill_boundary_layer(down , 1, 0, idy, idx, offset_y, offset_x, locate_p(block, j, i, 0));
if (idz == 1)
fill_boundary_layer(up , 1, 0, idy, idx, offset_y, offset_x, locate_p(block, j, i, block_shape[2] - 1));
#undef fill_boundary_layer
}
void Lab08::copy_boundary_layers_to_device(
std::vector<double> &left_h,
std::vector<double> &right_h,
std::vector<double> &front_h,
std::vector<double> &back_h,
std::vector<double> &down_h,
std::vector<double> &up_h
)
{
auto copy_boundary_layer_to_device =
[](
std::vector<double> &v_h,
CudaMemory<double> &v_d
)
{
v_d.memcpy(v_h.data(), hipMemcpyHostToDevice);
};
copy_boundary_layer_to_device(left_h, left_d);
copy_boundary_layer_to_device(right_h, right_d);
copy_boundary_layer_to_device(front_h, front_d);
copy_boundary_layer_to_device(back_h, back_d);
copy_boundary_layer_to_device(down_h, down_d);
copy_boundary_layer_to_device(up_h, up_d);
}
void Lab08::solve()
{
std::vector<double> left_h (block_x == 0 ? 0 : block_shape[1] * block_shape[2]),
right_h(block_x == process_grid_shape[0] - 1 ? 0 : block_shape[1] * block_shape[2]),
front_h(block_y == 0 ? 0 : block_shape[0] * block_shape[2]),
back_h (block_y == process_grid_shape[1] - 1 ? 0 : block_shape[0] * block_shape[2]),
down_h (block_z == 0 ? 0 : block_shape[0] * block_shape[1]),
up_h (block_z == process_grid_shape[2] - 1 ? 0 : block_shape[0] * block_shape[1]);
double n_x = block_shape[0] * process_grid_shape[0],
n_y = block_shape[1] * process_grid_shape[1],
n_z = block_shape[2] * process_grid_shape[2];
double h_x_pow_minus_2 = n_x * n_x / l[0] / l[0],
h_y_pow_minus_2 = n_y * n_y / l[1] / l[1],
h_z_pow_minus_2 = n_z * n_z / l[2] / l[2],
denominator = 2 * (h_x_pow_minus_2 + h_y_pow_minus_2 + h_z_pow_minus_2);
std::vector<MPI_Request> send_requests,
receive_requests;
checkCudaErrors(hipMemcpyToSymbol(
::block_shape,
block_shape.data(),
block_shape.size() * sizeof(decltype(block_shape[0])),
0,
hipMemcpyHostToDevice
));
thrust::device_ptr<double> abs_difference_array = thrust::device_pointer_cast(prev_block_d.get());
while (true)
{
CudaKernelChecker checker;
hipLaunchKernelGGL(( init_boundary_layers), dim3(GRID_SIZE_dim3), dim3(BLOCK_SIZE_dim3), 0, 0,
block_d.get(),
left_d.get(),
right_d.get(),
front_d.get(),
back_d.get(),
down_d.get(),
up_d.get()
);
checker.check("init_boundary_layers");
if (sends_first)
{
send_boundary_layers(send_requests);
checkMPIErrors(MPI_Waitall(
send_requests.size(),
send_requests.data(),
MPI_STATUSES_IGNORE
));
send_requests.clear();
receive_boundary_layers(
left_h,
right_h,
front_h,
back_h,
down_h,
up_h,
receive_requests
);
checkMPIErrors(MPI_Waitall(
receive_requests.size(),
receive_requests.data(),
MPI_STATUSES_IGNORE
));
receive_requests.clear();
}
else
{
receive_boundary_layers(
left_h,
right_h,
front_h,
back_h,
down_h,
up_h,
receive_requests
);
checkMPIErrors(MPI_Waitall(
receive_requests.size(),
receive_requests.data(),
MPI_STATUSES_IGNORE
));
receive_requests.clear();
send_boundary_layers(send_requests);
checkMPIErrors(MPI_Waitall(
send_requests.size(),
send_requests.data(),
MPI_STATUSES_IGNORE
));
send_requests.clear();
}
copy_boundary_layers_to_device(
left_h,
right_h,
front_h,
back_h,
down_h,
up_h
);
hipLaunchKernelGGL(( copy_block_to_prev_block), dim3(GRID_SIZE_dim3), dim3(BLOCK_SIZE_dim3), 0, 0,
block_d.get(),
prev_block_d.get()
);
checker.check("copy_block_to_prev_block");
hipLaunchKernelGGL(( block_iter_process), dim3(GRID_SIZE_dim3), dim3(BLOCK_SIZE_dim3), 0, 0,
block_d.get(),
prev_block_d.get(),
left_d.get(),
right_d.get(),
front_d.get(),
back_d.get(),
down_d.get(),
up_d.get(),
boundaries,
h_x_pow_minus_2,
h_y_pow_minus_2,
h_z_pow_minus_2,
denominator
);
checker.check("iter process kernel");
hipLaunchKernelGGL(( prev_block_to_abs_difference_array), dim3(GRID_SIZE_dim3), dim3(BLOCK_SIZE_dim3), 0, 0,
block_d.get(),
prev_block_d.get()
);
checker.check("prev_block_to_abs_difference_array");
double max_abs_difference = *thrust::max_element(
abs_difference_array,
abs_difference_array + prev_block_d.count
);
double total_max_abs_difference;
checkMPIErrors(MPI_Allreduce(
&max_abs_difference,
&total_max_abs_difference,
1,
MPI_DOUBLE,
MPI_MAX,
MPI_COMM_WORLD
));
if (total_max_abs_difference < eps)
break;
}
}
void Lab08::write_answer()
{
timer.stop();
if (rank == 0)
timer.print_time();
MPI_File file;
int delete_error = MPI_File_delete(output_name.c_str(), MPI_INFO_NULL);
if (delete_error != 0 && delete_error != MPI_ERR_NO_SUCH_FILE)
checkMPIErrors(delete_error);
checkMPIErrors(MPI_File_open(
MPI_COMM_WORLD,
output_name.c_str(),
MPI_MODE_CREATE | MPI_MODE_WRONLY,
MPI_INFO_NULL,
&file
));
// create type
MPI_Datatype Number;
const int number_chars_count = 16; // 0 . 000000 e+000 ' '
checkMPIErrors(MPI_Type_contiguous(
number_chars_count,
MPI_CHAR,
&Number
));
MPI_Datatype BlockRow;
checkMPIErrors(MPI_Type_contiguous(
block_shape[0],
Number,
&BlockRow
));
MPI_Datatype BlockPlane;
std::vector<int> BlockPlane_blocklengths;
std::vector<int> BlockPlane_displacements;
for (size_t i = 0; i < block_shape[1]; ++i)
{
BlockPlane_blocklengths.push_back(1);
BlockPlane_displacements.push_back(i * process_grid_shape[0]);
}
checkMPIErrors(MPI_Type_create_hvector(block_shape[1], 1, process_grid_shape[0] * block_shape[0] * number_chars_count, BlockRow, &BlockPlane));
/* checkMPIErrors(MPI_Type_indexed( */
/* block_shape[1], */
/* BlockPlane_blocklengths.data(), */
/* BlockPlane_displacements.data(), */
/* BlockRow, */
/* &BlockPlane */
/* )); */
MPI_Datatype Block;
std::vector<int> Block_blocklengths;
std::vector<int> Block_displacements;
for (size_t i = 0; i < block_shape[2]; ++i)
{
Block_blocklengths.push_back(1);
Block_displacements.push_back(i * process_grid_shape[1]);
}
checkMPIErrors(MPI_Type_create_hvector(block_shape[2], 1, process_grid_shape[1] * block_shape[0] * number_chars_count * process_grid_shape[0] * block_shape[1], BlockPlane, &Block));
/* checkMPIErrors(MPI_Type_indexed( */
/* block_shape[2], */
/* Block_blocklengths.data(), */
/* Block_displacements.data(), */
/* BlockPlane, */
/* &Block */
/* )); */
checkMPIErrors(MPI_Type_commit(&Block));
// set view with created type
MPI_Offset offset = 0;
offset += block_shape[0] * number_chars_count * block_x;
offset += block_shape[0] * block_shape[1] * process_grid_shape[0] * number_chars_count * block_y;
offset += block_shape[0] * block_shape[1] * block_shape[2] * process_grid_shape[0] * process_grid_shape[1] * number_chars_count * block_z;
checkMPIErrors(MPI_File_set_view(
file,
offset,
MPI_CHAR,
Block,
"native",
MPI_INFO_NULL
));
// create buffer with data to write
std::string buffer;
size_t buffer_pos = 0;
block_h.resize(block_d.count);
block_d.memcpy(block_h.data(), hipMemcpyDeviceToHost);
/* for (size_t i = 0; i < block_h.size(); ++i) */
/* { */
/* std::cout << i << std::endl; */
/* block_h[i] = i + block_h.size() * rank; */
/* } */
for (long long k = 0; k < block_shape[2]; ++k)
for (long long j = 0; j < block_shape[1]; ++j)
for (long long i = 0; i < block_shape[0]; ++i)
{
buffer.resize(buffer_pos + number_chars_count);
sprintf(&buffer[buffer_pos], "%-16e", locate(i, j, k));
buffer_pos += number_chars_count;
if (block_x == process_grid_shape[0] - 1 && i == block_shape[0] - 1)
{
buffer[buffer_pos - 1] = '\n';
if (j == block_shape[1] - 1 && block_y == process_grid_shape[1] - 1 && (block_z != process_grid_shape[2] - 1 || k != block_shape[2] - 1))
buffer[buffer_pos - 2] = '\n';
}
}
/* buffer[0] = 'R'; */
/* buffer[1] = 'a'; */
/* buffer[2] = 'n'; */
/* buffer[3] = 'k'; */
/* buffer[4] = ' '; */
/* buffer[5] = '0' + rank; */
/* if (rank == 0) { */
/* std::cout << buffer << std::endl; */
/* for (int i = 1; i < 8; i++) { */
/* MPI_Recv(&buffer[0], buffer.size(), MPI_CHAR, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); */
/* std::cout << std::endl << buffer << std::endl; */
/* } */
/* } else { */
/* MPI_Send(&buffer[0], buffer.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD); */
/* } */
// write data from buffer
checkMPIErrors(MPI_File_write_all(
file,
buffer.data(),
buffer.size(),
MPI_CHAR,
MPI_STATUS_IGNORE
));
// close file
checkMPIErrors(MPI_File_close(&file));
}
| 464e3d1584f0013dfda51116d9aaaf21bd91bc13.cu | #include "lab08.cuh"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cstdio>
#include "MPI_dummy_helper.hpp"
#include "dummy_helper.cuh"
#include <mpi.h>
#include <cuda_runtime.h>
#include <thrust/device_vector.h>
#include <thrust/extrema.h>
#define GRID_SIZE 1
#define BLOCK_SIZE 4
#define GRID_SIZE_dim3 dim3(GRID_SIZE, GRID_SIZE, GRID_SIZE)
#define BLOCK_SIZE_dim3 dim3(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
#define locate(i, j, k) block_h[(i) + (j) * block_shape[0] + (k) * block_shape[0] * block_shape[1]]
#define locate_p(v, i, j, k) v[(i) + (j) * block_shape[0] + (k) * block_shape[0] * block_shape[1]]
__global__
void init_array(
double *v,
long long count,
double init_value
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long id = idx + idy * blockDim.x * gridDim.x + idz * blockDim.x * gridDim.x * blockDim.y * gridDim.y;
const long long offset = gridDim.x * blockDim.x * gridDim.y * blockDim.y * gridDim.z * blockDim.z;
for (long long i = id; i < count; i += offset)
v[i] = init_value;
}
void Lab08::set_device()
{
int device_count;
checkCudaErrors(cudaGetDeviceCount(&device_count));
checkCudaErrors(cudaSetDevice(rank % device_count));
}
Lab08::Lab08(int argc, char **argv)
{
init(argc, argv);
checkMPIErrors(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
set_device();
// read input data
if (rank == 0)
rank_0_init();
else
rank_non_0_init();
block_z = rank / process_grid_shape[0] / process_grid_shape[1];
block_y = rank % (process_grid_shape[0] * process_grid_shape[1]) / process_grid_shape[0];
block_x = rank % (process_grid_shape[0] * process_grid_shape[1]) % process_grid_shape[0];
sends_first = (block_x + block_y + block_z) % 2;
CudaKernelChecker kernel_checker;
auto block_init_routine =
[&kernel_checker, this](CudaMemory<double> &v, const char* kernel_name)
{
v.alloc(block_shape[0] * block_shape[1] * block_shape[2]);
init_array<<<GRID_SIZE_dim3, BLOCK_SIZE_dim3>>>(
v.get(),
block_shape[0] * block_shape[1] * block_shape[2],
u_0
);
kernel_checker.check(kernel_name);
};
block_init_routine(block_d, "init block_d");
block_init_routine(prev_block_d, "init prev_block_d");
auto boundary_layer_init_routine =
[this](
std::vector<double> &v_h,
CudaMemory<double> &v_d,
const bool layer_not_needed,
const long long count
)
{
v_h.resize( layer_not_needed ? 0 : count);
v_d.alloc( layer_not_needed ? 0 : count);
};
boundary_layer_init_routine(
left_h,
left_d,
block_x == 0,
block_shape[1] * block_shape[2]
);
boundary_layer_init_routine(
right_h,
right_d,
block_x == process_grid_shape[0] - 1,
block_shape[1] * block_shape[2]
);
boundary_layer_init_routine(
front_h,
front_d,
block_y == 0,
block_shape[0] * block_shape[2]
);
boundary_layer_init_routine(
back_h,
back_d,
block_y == process_grid_shape[1] - 1,
block_shape[0] * block_shape[2]
);
boundary_layer_init_routine(
down_h,
down_d,
block_z == 0,
block_shape[0] * block_shape[1]
);
boundary_layer_init_routine(
up_h,
up_d,
block_z == process_grid_shape[2] - 1,
block_shape[0] * block_shape[1]
);
timer.start();
}
void Lab08::init(int argc, char **argv)
{
int initialized;
checkMPIErrors(MPI_Initialized(
&initialized
));
if (!initialized)
{
checkMPIErrors(MPI_Init(&argc, &argv));
}
}
void Lab08::finalize()
{
int finalized;
checkMPIErrors(MPI_Finalized(
&finalized
));
if (!finalized)
{
checkMPIErrors(MPI_Barrier(MPI_COMM_WORLD));
checkMPIErrors(MPI_Finalize());
}
}
void Lab08::rank_0_init()
{
// input
read_in_container(process_grid_shape);
read_in_container(block_shape);
std::cin >> output_name;
std::cin >> eps;
read_in_container(l);
std::cin >> boundaries.down
>> boundaries.up
>> boundaries.left
>> boundaries.right
>> boundaries.front
>> boundaries.back;
std::cin >> u_0;
// input done
// send input data to other ranks
int n_ranks = process_grid_shape[0]
* process_grid_shape[1]
* process_grid_shape[2];
for (int rank = 1; rank < n_ranks; ++rank)
{
checkMPIErrors(MPI_Send(
process_grid_shape.data(),
process_grid_shape.size(),
MPI_LONG_LONG,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
block_shape.data(),
block_shape.size(),
MPI_LONG_LONG,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
output_name.data(),
output_name.size(),
MPI_CHAR,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
&eps,
1,
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
l.data(),
l.size(),
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
reinterpret_cast<double*>(&boundaries),
sizeof(boundaries) / sizeof(double),
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
checkMPIErrors(MPI_Send(
&u_0,
1,
MPI_DOUBLE,
rank,
SEND_ANY_TAG,
MPI_COMM_WORLD
));
}
}
void Lab08::rank_non_0_init()
{
int root_rank = 0;
checkMPIErrors(MPI_Recv(
process_grid_shape.data(),
process_grid_shape.size(),
MPI_LONG_LONG,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
block_shape.data(),
block_shape.size(),
MPI_LONG_LONG,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
MPI_Status status;
checkMPIErrors(MPI_Probe(
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
&status
));
int output_name_count;
checkMPIErrors(MPI_Get_count(
&status,
MPI_CHAR,
&output_name_count
));
std::vector<char> output_name_buffer(output_name_count);
checkMPIErrors(MPI_Recv(
output_name_buffer.data(),
output_name_buffer.size(),
MPI_CHAR,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
std::copy(
output_name_buffer.begin(),
output_name_buffer.end(),
std::back_inserter(output_name)
);
checkMPIErrors(MPI_Recv(
&eps,
1,
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
l.data(),
l.size(),
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
reinterpret_cast<double*>(&boundaries),
sizeof(boundaries) / sizeof(double),
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
checkMPIErrors(MPI_Recv(
&u_0,
1,
MPI_DOUBLE,
root_rank,
MPI_ANY_TAG,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE
));
}
int Lab08::block_position_to_rank(
long long block_x,
long long block_y,
long long block_z
)
{
if (block_x < 0 || block_y < 0 || block_z < 0)
{
return -1;
}
if (
block_x >= process_grid_shape[0]
|| block_y >= process_grid_shape[1]
|| block_z >= process_grid_shape[2]
)
{
return -1;
}
return block_x
+ block_y * process_grid_shape[0]
+ block_z * process_grid_shape[0] * process_grid_shape[1];
}
void Lab08::send_boundary_layer(
std::vector<double> &v_h,
CudaMemory<double> &v_d,
int destination_rank,
Lab08::BoundaryLayerTag tag,
std::vector<MPI_Request> &send_requests
)
{
if (destination_rank > -1)
{
v_d.memcpy(
v_h.data(),
cudaMemcpyDeviceToHost
);
MPI_Request request;
checkMPIErrors(MPI_Isend(
v_h.data(),
v_h.size(),
MPI_DOUBLE,
destination_rank,
static_cast<int>(tag),
MPI_COMM_WORLD,
&request
));
send_requests.push_back(request);
}
}
void Lab08::send_boundary_layers(std::vector<MPI_Request> &send_requests)
{
send_boundary_layer(
left_h,
left_d,
block_position_to_rank(block_x - 1, block_y, block_z),
BoundaryLayerTag::RIGHT,
send_requests
);
send_boundary_layer(
right_h,
right_d,
block_position_to_rank(block_x + 1, block_y, block_z),
BoundaryLayerTag::LEFT,
send_requests
);
send_boundary_layer(
front_h,
front_d,
block_position_to_rank(block_x, block_y - 1, block_z),
BoundaryLayerTag::BACK,
send_requests
);
send_boundary_layer(
back_h,
back_d,
block_position_to_rank(block_x, block_y + 1, block_z),
BoundaryLayerTag::FRONT,
send_requests
);
send_boundary_layer(
down_h,
down_d,
block_position_to_rank(block_x, block_y, block_z - 1),
BoundaryLayerTag::UP,
send_requests
);
send_boundary_layer(
up_h,
up_d,
block_position_to_rank(block_x, block_y, block_z + 1),
BoundaryLayerTag::DOWN,
send_requests
);
}
void Lab08::receive_boundary_layer(
std::vector<double> &v,
int source_rank,
BoundaryLayerTag tag,
std::vector<MPI_Request> &receive_requests
)
{
if (source_rank > -1)
{
MPI_Request request;
checkMPIErrors(MPI_Irecv(
v.data(),
v.size(),
MPI_DOUBLE,
source_rank,
static_cast<int>(tag),
MPI_COMM_WORLD,
&request
));
receive_requests.push_back(request);
}
}
void Lab08::receive_boundary_layers(
std::vector<double> &left_h,
std::vector<double> &right_h,
std::vector<double> &front_h,
std::vector<double> &back_h,
std::vector<double> &down_h,
std::vector<double> &up_h,
std::vector<MPI_Request> &receive_requests
)
{
receive_boundary_layer(
left_h,
block_position_to_rank(block_x - 1, block_y, block_z),
BoundaryLayerTag::LEFT,
receive_requests
);
receive_boundary_layer(
right_h,
block_position_to_rank(block_x + 1, block_y, block_z),
BoundaryLayerTag::RIGHT,
receive_requests
);
receive_boundary_layer(
front_h,
block_position_to_rank(block_x, block_y - 1, block_z),
BoundaryLayerTag::FRONT,
receive_requests
);
receive_boundary_layer(
back_h,
block_position_to_rank(block_x, block_y + 1, block_z),
BoundaryLayerTag::BACK,
receive_requests
);
receive_boundary_layer(
down_h,
block_position_to_rank(block_x, block_y, block_z - 1),
BoundaryLayerTag::DOWN,
receive_requests
);
receive_boundary_layer(
up_h,
block_position_to_rank(block_x, block_y, block_z + 1),
BoundaryLayerTag::UP,
receive_requests
);
}
__device__ long long block_shape[3];
__global__
void copy_block_to_prev_block(
double *block,
double *prev_block
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
for (long long k = idz; k < block_shape[2]; k += offset_z)
for (long long j = idy; j < block_shape[1]; j += offset_y)
for (long long i = idx; i < block_shape[0]; i += offset_x)
locate_p(prev_block, i, j, k) = locate_p(block, i, j, k);
}
__global__
void prev_block_to_abs_difference_array(
double *block,
double *prev_block
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
for (long long k = idz; k < block_shape[2]; k += offset_z)
for (long long j = idy; j < block_shape[1]; j += offset_y)
for (long long i = idx; i < block_shape[0]; i += offset_x)
locate_p(prev_block, i, j, k) = fabs(locate_p(block, i, j, k) - locate_p(prev_block, i, j, k));
}
__global__
void block_iter_process(
double *block,
double *prev_block,
double *left,
double *right,
double *front,
double *back,
double *down,
double *up,
Lab08::Boundaries boundaries,
double h_x_pow_minus_2,
double h_y_pow_minus_2,
double h_z_pow_minus_2,
double denominator
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
for (long long k = idz; k < block_shape[2]; k += offset_z)
for (long long j = idy; j < block_shape[1]; j += offset_y)
for (long long i = idx; i < block_shape[0]; i += offset_x)
{
double u_left = i == 0 ? (left == nullptr ? boundaries.left : left[ j + block_shape[1] * k]) : locate_p(prev_block, i - 1, j, k),
u_right = i == block_shape[0] - 1 ? (right == nullptr ? boundaries.right : right[j + block_shape[1] * k]) : locate_p(prev_block, i + 1, j, k),
u_front = j == 0 ? (front == nullptr ? boundaries.front : front[i + block_shape[0] * k]) : locate_p(prev_block, i, j - 1, k),
u_back = j == block_shape[1] - 1 ? (back == nullptr ? boundaries.back : back[ i + block_shape[0] * k]) : locate_p(prev_block, i, j + 1, k),
u_down = k == 0 ? (down == nullptr ? boundaries.down : down[ i + block_shape[0] * j]) : locate_p(prev_block, i, j, k - 1),
u_up = k == block_shape[2] - 1 ? (up == nullptr ? boundaries.up : up[ i + block_shape[0] * j]) : locate_p(prev_block, i, j, k + 1);
locate_p(block, i, j, k) = (u_left + u_right) * h_x_pow_minus_2;
locate_p(block, i, j, k) += (u_front + u_back ) * h_y_pow_minus_2;
locate_p(block, i, j, k) += (u_down + u_up ) * h_z_pow_minus_2;
locate_p(block, i, j, k) /= denominator;
}
}
__global__
void init_boundary_layers(
double *block,
double *left,
double *right,
double *front,
double *back,
double *down,
double *up
)
{
const long long idx = blockDim.x * blockIdx.x + threadIdx.x;
const long long idy = blockDim.y * blockIdx.y + threadIdx.y;
const long long idz = blockDim.z * blockIdx.z + threadIdx.z;
const long long offset_x = blockDim.x * gridDim.x;
const long long offset_y = blockDim.y * gridDim.y;
const long long offset_z = blockDim.z * gridDim.z;
#define fill_boundary_layer(v, outer, inner, outer_start, inner_start, outer_offset, inner_offset, loc) \
{ \
if (v) \
{ \
for (long long i = outer_start; i < block_shape[outer]; i += outer_offset) \
for (long long j = inner_start; j < block_shape[inner]; j += inner_offset) \
v[i * block_shape[inner] + j] = loc; \
} \
} \
if (idx == 0)
fill_boundary_layer(left , 2, 1, idz, idy, offset_z, offset_y, locate_p(block, 0, j, i));
if (idx == 1)
fill_boundary_layer(right, 2, 1, idz, idy, offset_z, offset_y, locate_p(block, block_shape[0] - 1, j, i));
if (idy == 0)
fill_boundary_layer(front, 2, 0, idz, idx, offset_z, offset_x, locate_p(block, j, 0, i));
if (idy == 1)
fill_boundary_layer(back , 2, 0, idz, idx, offset_z, offset_x, locate_p(block, j, block_shape[1] - 1, i));
if (idz == 0)
fill_boundary_layer(down , 1, 0, idy, idx, offset_y, offset_x, locate_p(block, j, i, 0));
if (idz == 1)
fill_boundary_layer(up , 1, 0, idy, idx, offset_y, offset_x, locate_p(block, j, i, block_shape[2] - 1));
#undef fill_boundary_layer
}
void Lab08::copy_boundary_layers_to_device(
std::vector<double> &left_h,
std::vector<double> &right_h,
std::vector<double> &front_h,
std::vector<double> &back_h,
std::vector<double> &down_h,
std::vector<double> &up_h
)
{
auto copy_boundary_layer_to_device =
[](
std::vector<double> &v_h,
CudaMemory<double> &v_d
)
{
v_d.memcpy(v_h.data(), cudaMemcpyHostToDevice);
};
copy_boundary_layer_to_device(left_h, left_d);
copy_boundary_layer_to_device(right_h, right_d);
copy_boundary_layer_to_device(front_h, front_d);
copy_boundary_layer_to_device(back_h, back_d);
copy_boundary_layer_to_device(down_h, down_d);
copy_boundary_layer_to_device(up_h, up_d);
}
void Lab08::solve()
{
std::vector<double> left_h (block_x == 0 ? 0 : block_shape[1] * block_shape[2]),
right_h(block_x == process_grid_shape[0] - 1 ? 0 : block_shape[1] * block_shape[2]),
front_h(block_y == 0 ? 0 : block_shape[0] * block_shape[2]),
back_h (block_y == process_grid_shape[1] - 1 ? 0 : block_shape[0] * block_shape[2]),
down_h (block_z == 0 ? 0 : block_shape[0] * block_shape[1]),
up_h (block_z == process_grid_shape[2] - 1 ? 0 : block_shape[0] * block_shape[1]);
double n_x = block_shape[0] * process_grid_shape[0],
n_y = block_shape[1] * process_grid_shape[1],
n_z = block_shape[2] * process_grid_shape[2];
double h_x_pow_minus_2 = n_x * n_x / l[0] / l[0],
h_y_pow_minus_2 = n_y * n_y / l[1] / l[1],
h_z_pow_minus_2 = n_z * n_z / l[2] / l[2],
denominator = 2 * (h_x_pow_minus_2 + h_y_pow_minus_2 + h_z_pow_minus_2);
std::vector<MPI_Request> send_requests,
receive_requests;
checkCudaErrors(cudaMemcpyToSymbol(
::block_shape,
block_shape.data(),
block_shape.size() * sizeof(decltype(block_shape[0])),
0,
cudaMemcpyHostToDevice
));
thrust::device_ptr<double> abs_difference_array = thrust::device_pointer_cast(prev_block_d.get());
while (true)
{
CudaKernelChecker checker;
init_boundary_layers<<<GRID_SIZE_dim3, BLOCK_SIZE_dim3>>>(
block_d.get(),
left_d.get(),
right_d.get(),
front_d.get(),
back_d.get(),
down_d.get(),
up_d.get()
);
checker.check("init_boundary_layers");
if (sends_first)
{
send_boundary_layers(send_requests);
checkMPIErrors(MPI_Waitall(
send_requests.size(),
send_requests.data(),
MPI_STATUSES_IGNORE
));
send_requests.clear();
receive_boundary_layers(
left_h,
right_h,
front_h,
back_h,
down_h,
up_h,
receive_requests
);
checkMPIErrors(MPI_Waitall(
receive_requests.size(),
receive_requests.data(),
MPI_STATUSES_IGNORE
));
receive_requests.clear();
}
else
{
receive_boundary_layers(
left_h,
right_h,
front_h,
back_h,
down_h,
up_h,
receive_requests
);
checkMPIErrors(MPI_Waitall(
receive_requests.size(),
receive_requests.data(),
MPI_STATUSES_IGNORE
));
receive_requests.clear();
send_boundary_layers(send_requests);
checkMPIErrors(MPI_Waitall(
send_requests.size(),
send_requests.data(),
MPI_STATUSES_IGNORE
));
send_requests.clear();
}
copy_boundary_layers_to_device(
left_h,
right_h,
front_h,
back_h,
down_h,
up_h
);
copy_block_to_prev_block<<<GRID_SIZE_dim3, BLOCK_SIZE_dim3>>>(
block_d.get(),
prev_block_d.get()
);
checker.check("copy_block_to_prev_block");
block_iter_process<<<GRID_SIZE_dim3, BLOCK_SIZE_dim3>>>(
block_d.get(),
prev_block_d.get(),
left_d.get(),
right_d.get(),
front_d.get(),
back_d.get(),
down_d.get(),
up_d.get(),
boundaries,
h_x_pow_minus_2,
h_y_pow_minus_2,
h_z_pow_minus_2,
denominator
);
checker.check("iter process kernel");
prev_block_to_abs_difference_array<<<GRID_SIZE_dim3, BLOCK_SIZE_dim3>>>(
block_d.get(),
prev_block_d.get()
);
checker.check("prev_block_to_abs_difference_array");
double max_abs_difference = *thrust::max_element(
abs_difference_array,
abs_difference_array + prev_block_d.count
);
double total_max_abs_difference;
checkMPIErrors(MPI_Allreduce(
&max_abs_difference,
&total_max_abs_difference,
1,
MPI_DOUBLE,
MPI_MAX,
MPI_COMM_WORLD
));
if (total_max_abs_difference < eps)
break;
}
}
void Lab08::write_answer()
{
timer.stop();
if (rank == 0)
timer.print_time();
MPI_File file;
int delete_error = MPI_File_delete(output_name.c_str(), MPI_INFO_NULL);
if (delete_error != 0 && delete_error != MPI_ERR_NO_SUCH_FILE)
checkMPIErrors(delete_error);
checkMPIErrors(MPI_File_open(
MPI_COMM_WORLD,
output_name.c_str(),
MPI_MODE_CREATE | MPI_MODE_WRONLY,
MPI_INFO_NULL,
&file
));
// create type
MPI_Datatype Number;
const int number_chars_count = 16; // 0 . 000000 e+000 ' '
checkMPIErrors(MPI_Type_contiguous(
number_chars_count,
MPI_CHAR,
&Number
));
MPI_Datatype BlockRow;
checkMPIErrors(MPI_Type_contiguous(
block_shape[0],
Number,
&BlockRow
));
MPI_Datatype BlockPlane;
std::vector<int> BlockPlane_blocklengths;
std::vector<int> BlockPlane_displacements;
for (size_t i = 0; i < block_shape[1]; ++i)
{
BlockPlane_blocklengths.push_back(1);
BlockPlane_displacements.push_back(i * process_grid_shape[0]);
}
checkMPIErrors(MPI_Type_create_hvector(block_shape[1], 1, process_grid_shape[0] * block_shape[0] * number_chars_count, BlockRow, &BlockPlane));
/* checkMPIErrors(MPI_Type_indexed( */
/* block_shape[1], */
/* BlockPlane_blocklengths.data(), */
/* BlockPlane_displacements.data(), */
/* BlockRow, */
/* &BlockPlane */
/* )); */
MPI_Datatype Block;
std::vector<int> Block_blocklengths;
std::vector<int> Block_displacements;
for (size_t i = 0; i < block_shape[2]; ++i)
{
Block_blocklengths.push_back(1);
Block_displacements.push_back(i * process_grid_shape[1]);
}
checkMPIErrors(MPI_Type_create_hvector(block_shape[2], 1, process_grid_shape[1] * block_shape[0] * number_chars_count * process_grid_shape[0] * block_shape[1], BlockPlane, &Block));
/* checkMPIErrors(MPI_Type_indexed( */
/* block_shape[2], */
/* Block_blocklengths.data(), */
/* Block_displacements.data(), */
/* BlockPlane, */
/* &Block */
/* )); */
checkMPIErrors(MPI_Type_commit(&Block));
// set view with created type
MPI_Offset offset = 0;
offset += block_shape[0] * number_chars_count * block_x;
offset += block_shape[0] * block_shape[1] * process_grid_shape[0] * number_chars_count * block_y;
offset += block_shape[0] * block_shape[1] * block_shape[2] * process_grid_shape[0] * process_grid_shape[1] * number_chars_count * block_z;
checkMPIErrors(MPI_File_set_view(
file,
offset,
MPI_CHAR,
Block,
"native",
MPI_INFO_NULL
));
// create buffer with data to write
std::string buffer;
size_t buffer_pos = 0;
block_h.resize(block_d.count);
block_d.memcpy(block_h.data(), cudaMemcpyDeviceToHost);
/* for (size_t i = 0; i < block_h.size(); ++i) */
/* { */
/* std::cout << i << std::endl; */
/* block_h[i] = i + block_h.size() * rank; */
/* } */
for (long long k = 0; k < block_shape[2]; ++k)
for (long long j = 0; j < block_shape[1]; ++j)
for (long long i = 0; i < block_shape[0]; ++i)
{
buffer.resize(buffer_pos + number_chars_count);
sprintf(&buffer[buffer_pos], "%-16e", locate(i, j, k));
buffer_pos += number_chars_count;
if (block_x == process_grid_shape[0] - 1 && i == block_shape[0] - 1)
{
buffer[buffer_pos - 1] = '\n';
if (j == block_shape[1] - 1 && block_y == process_grid_shape[1] - 1 && (block_z != process_grid_shape[2] - 1 || k != block_shape[2] - 1))
buffer[buffer_pos - 2] = '\n';
}
}
/* buffer[0] = 'R'; */
/* buffer[1] = 'a'; */
/* buffer[2] = 'n'; */
/* buffer[3] = 'k'; */
/* buffer[4] = ' '; */
/* buffer[5] = '0' + rank; */
/* if (rank == 0) { */
/* std::cout << buffer << std::endl; */
/* for (int i = 1; i < 8; i++) { */
/* MPI_Recv(&buffer[0], buffer.size(), MPI_CHAR, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); */
/* std::cout << std::endl << buffer << std::endl; */
/* } */
/* } else { */
/* MPI_Send(&buffer[0], buffer.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD); */
/* } */
// write data from buffer
checkMPIErrors(MPI_File_write_all(
file,
buffer.data(),
buffer.size(),
MPI_CHAR,
MPI_STATUS_IGNORE
));
// close file
checkMPIErrors(MPI_File_close(&file));
}
|
21a2390523ec5438ddf21ad9d91b04ae044ec944.hip | // !!! This is a file automatically generated by hipify!!!
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "../test_utils.cuh"
#include <gtest/gtest.h>
#include <raft/core/device_mdarray.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resource/thrust_policy.hpp>
#include <raft/matrix/copy.cuh>
#include <raft/random/rng.cuh>
#include <raft/util/cudart_utils.hpp>
#include <rmm/device_uvector.hpp>
#include <thrust/copy.h>
#include <thrust/device_ptr.h>
#include <thrust/iterator/counting_iterator.h>
namespace raft {
namespace matrix {
template <typename T>
struct MatrixInputs {
T tolerance;
int n_row;
int n_col;
unsigned long long int seed;
};
template <typename T>
::std::ostream& operator<<(::std::ostream& os, const MatrixInputs<T>& dims)
{
return os;
}
template <typename T>
class MatrixTest : public ::testing::TestWithParam<MatrixInputs<T>> {
public:
MatrixTest()
: params(::testing::TestWithParam<MatrixInputs<T>>::GetParam()),
stream(resource::get_cuda_stream(handle)),
in1(params.n_row * params.n_col, stream),
in2(params.n_row * params.n_col, stream),
in1_revr(params.n_row * params.n_col, stream)
{
}
protected:
void SetUp() override
{
raft::random::RngState r(params.seed);
int len = params.n_row * params.n_col;
uniform(handle, r, in1.data(), len, T(-1.0), T(1.0));
auto in1_view = raft::make_device_matrix_view<const T, int, col_major>(
in1.data(), params.n_row, params.n_col);
auto in2_view =
raft::make_device_matrix_view<T, int, col_major>(in2.data(), params.n_row, params.n_col);
copy<T, int>(handle, in1_view, in2_view);
// copy(in1, in1_revr, params.n_row, params.n_col);
// colReverse(in1_revr, params.n_row, params.n_col);
rmm::device_uvector<T> outTrunc(6, stream);
auto out_trunc_view = raft::make_device_matrix_view<T, int, col_major>(outTrunc.data(), 3, 2);
trunc_zero_origin<T, int>(handle, in1_view, out_trunc_view);
resource::sync_stream(handle, stream);
}
protected:
raft::resources handle;
hipStream_t stream;
MatrixInputs<T> params;
rmm::device_uvector<T> in1, in2, in1_revr;
};
const std::vector<MatrixInputs<float>> inputsf2 = {{0.000001f, 4, 4, 1234ULL}};
const std::vector<MatrixInputs<double>> inputsd2 = {{0.00000001, 4, 4, 1234ULL}};
typedef MatrixTest<float> MatrixTestF;
TEST_P(MatrixTestF, Result)
{
ASSERT_TRUE(raft::devArrMatch(in1.data(),
in2.data(),
params.n_row * params.n_col,
raft::CompareApprox<float>(params.tolerance),
stream));
}
typedef MatrixTest<double> MatrixTestD;
TEST_P(MatrixTestD, Result)
{
ASSERT_TRUE(raft::devArrMatch(in1.data(),
in2.data(),
params.n_row * params.n_col,
raft::CompareApprox<double>(params.tolerance),
stream));
}
INSTANTIATE_TEST_SUITE_P(MatrixTests, MatrixTestF, ::testing::ValuesIn(inputsf2));
INSTANTIATE_TEST_SUITE_P(MatrixTests, MatrixTestD, ::testing::ValuesIn(inputsd2));
template <typename T>
class MatrixCopyRowsTest : public ::testing::Test {
using math_t = typename std::tuple_element<0, T>::type;
using idx_t = typename std::tuple_element<1, T>::type;
using idx_array_t = typename std::tuple_element<2, T>::type;
protected:
MatrixCopyRowsTest()
: stream(resource::get_cuda_stream(handle)),
input(n_cols * n_rows, resource::get_cuda_stream(handle)),
indices(n_selected, resource::get_cuda_stream(handle)),
output(n_cols * n_selected, resource::get_cuda_stream(handle))
{
raft::update_device(indices.data(), indices_host, n_selected, stream);
// Init input array
thrust::counting_iterator<idx_t> first(0);
thrust::device_ptr<math_t> ptr(input.data());
thrust::copy(resource::get_thrust_policy(handle), first, first + n_cols * n_rows, ptr);
}
void testCopyRows()
{
auto input_view = raft::make_device_matrix_view<const math_t, idx_array_t, col_major>(
input.data(), n_rows, n_cols);
auto output_view = raft::make_device_matrix_view<math_t, idx_array_t, col_major>(
output.data(), n_selected, n_cols);
auto indices_view =
raft::make_device_vector_view<const idx_array_t, idx_array_t>(indices.data(), n_selected);
raft::matrix::copy_rows(handle, input_view, output_view, indices_view);
EXPECT_TRUE(raft::devArrMatchHost(
output_exp_colmajor, output.data(), n_selected * n_cols, raft::Compare<math_t>(), stream));
auto input_row_view = raft::make_device_matrix_view<const math_t, idx_array_t, row_major>(
input.data(), n_rows, n_cols);
auto output_row_view = raft::make_device_matrix_view<math_t, idx_array_t, row_major>(
output.data(), n_selected, n_cols);
raft::matrix::copy_rows(handle, input_row_view, output_row_view, indices_view);
EXPECT_TRUE(raft::devArrMatchHost(
output_exp_rowmajor, output.data(), n_selected * n_cols, raft::Compare<math_t>(), stream));
}
protected:
raft::resources handle;
hipStream_t stream;
int n_rows = 10;
int n_cols = 3;
int n_selected = 5;
idx_array_t indices_host[5] = {0, 3, 4, 7, 9};
math_t output_exp_colmajor[15] = {0, 3, 4, 7, 9, 10, 13, 14, 17, 19, 20, 23, 24, 27, 29};
math_t output_exp_rowmajor[15] = {0, 1, 2, 9, 10, 11, 12, 13, 14, 21, 22, 23, 27, 28, 29};
rmm::device_uvector<math_t> input;
rmm::device_uvector<math_t> output;
rmm::device_uvector<idx_array_t> indices;
};
using TypeTuple = ::testing::Types<std::tuple<float, int, int>,
std::tuple<float, int64_t, int>,
std::tuple<double, int, int>,
std::tuple<double, int64_t, int>>;
TYPED_TEST_CASE(MatrixCopyRowsTest, TypeTuple);
TYPED_TEST(MatrixCopyRowsTest, CopyRows) { this->testCopyRows(); }
} // namespace matrix
} // namespace raft
| 21a2390523ec5438ddf21ad9d91b04ae044ec944.cu | /*
* Copyright (c) 2018-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "../test_utils.cuh"
#include <gtest/gtest.h>
#include <raft/core/device_mdarray.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resource/thrust_policy.hpp>
#include <raft/matrix/copy.cuh>
#include <raft/random/rng.cuh>
#include <raft/util/cudart_utils.hpp>
#include <rmm/device_uvector.hpp>
#include <thrust/copy.h>
#include <thrust/device_ptr.h>
#include <thrust/iterator/counting_iterator.h>
namespace raft {
namespace matrix {
template <typename T>
struct MatrixInputs {
T tolerance;
int n_row;
int n_col;
unsigned long long int seed;
};
template <typename T>
::std::ostream& operator<<(::std::ostream& os, const MatrixInputs<T>& dims)
{
return os;
}
template <typename T>
class MatrixTest : public ::testing::TestWithParam<MatrixInputs<T>> {
public:
MatrixTest()
: params(::testing::TestWithParam<MatrixInputs<T>>::GetParam()),
stream(resource::get_cuda_stream(handle)),
in1(params.n_row * params.n_col, stream),
in2(params.n_row * params.n_col, stream),
in1_revr(params.n_row * params.n_col, stream)
{
}
protected:
void SetUp() override
{
raft::random::RngState r(params.seed);
int len = params.n_row * params.n_col;
uniform(handle, r, in1.data(), len, T(-1.0), T(1.0));
auto in1_view = raft::make_device_matrix_view<const T, int, col_major>(
in1.data(), params.n_row, params.n_col);
auto in2_view =
raft::make_device_matrix_view<T, int, col_major>(in2.data(), params.n_row, params.n_col);
copy<T, int>(handle, in1_view, in2_view);
// copy(in1, in1_revr, params.n_row, params.n_col);
// colReverse(in1_revr, params.n_row, params.n_col);
rmm::device_uvector<T> outTrunc(6, stream);
auto out_trunc_view = raft::make_device_matrix_view<T, int, col_major>(outTrunc.data(), 3, 2);
trunc_zero_origin<T, int>(handle, in1_view, out_trunc_view);
resource::sync_stream(handle, stream);
}
protected:
raft::resources handle;
cudaStream_t stream;
MatrixInputs<T> params;
rmm::device_uvector<T> in1, in2, in1_revr;
};
const std::vector<MatrixInputs<float>> inputsf2 = {{0.000001f, 4, 4, 1234ULL}};
const std::vector<MatrixInputs<double>> inputsd2 = {{0.00000001, 4, 4, 1234ULL}};
typedef MatrixTest<float> MatrixTestF;
TEST_P(MatrixTestF, Result)
{
ASSERT_TRUE(raft::devArrMatch(in1.data(),
in2.data(),
params.n_row * params.n_col,
raft::CompareApprox<float>(params.tolerance),
stream));
}
typedef MatrixTest<double> MatrixTestD;
TEST_P(MatrixTestD, Result)
{
ASSERT_TRUE(raft::devArrMatch(in1.data(),
in2.data(),
params.n_row * params.n_col,
raft::CompareApprox<double>(params.tolerance),
stream));
}
INSTANTIATE_TEST_SUITE_P(MatrixTests, MatrixTestF, ::testing::ValuesIn(inputsf2));
INSTANTIATE_TEST_SUITE_P(MatrixTests, MatrixTestD, ::testing::ValuesIn(inputsd2));
template <typename T>
class MatrixCopyRowsTest : public ::testing::Test {
using math_t = typename std::tuple_element<0, T>::type;
using idx_t = typename std::tuple_element<1, T>::type;
using idx_array_t = typename std::tuple_element<2, T>::type;
protected:
MatrixCopyRowsTest()
: stream(resource::get_cuda_stream(handle)),
input(n_cols * n_rows, resource::get_cuda_stream(handle)),
indices(n_selected, resource::get_cuda_stream(handle)),
output(n_cols * n_selected, resource::get_cuda_stream(handle))
{
raft::update_device(indices.data(), indices_host, n_selected, stream);
// Init input array
thrust::counting_iterator<idx_t> first(0);
thrust::device_ptr<math_t> ptr(input.data());
thrust::copy(resource::get_thrust_policy(handle), first, first + n_cols * n_rows, ptr);
}
void testCopyRows()
{
auto input_view = raft::make_device_matrix_view<const math_t, idx_array_t, col_major>(
input.data(), n_rows, n_cols);
auto output_view = raft::make_device_matrix_view<math_t, idx_array_t, col_major>(
output.data(), n_selected, n_cols);
auto indices_view =
raft::make_device_vector_view<const idx_array_t, idx_array_t>(indices.data(), n_selected);
raft::matrix::copy_rows(handle, input_view, output_view, indices_view);
EXPECT_TRUE(raft::devArrMatchHost(
output_exp_colmajor, output.data(), n_selected * n_cols, raft::Compare<math_t>(), stream));
auto input_row_view = raft::make_device_matrix_view<const math_t, idx_array_t, row_major>(
input.data(), n_rows, n_cols);
auto output_row_view = raft::make_device_matrix_view<math_t, idx_array_t, row_major>(
output.data(), n_selected, n_cols);
raft::matrix::copy_rows(handle, input_row_view, output_row_view, indices_view);
EXPECT_TRUE(raft::devArrMatchHost(
output_exp_rowmajor, output.data(), n_selected * n_cols, raft::Compare<math_t>(), stream));
}
protected:
raft::resources handle;
cudaStream_t stream;
int n_rows = 10;
int n_cols = 3;
int n_selected = 5;
idx_array_t indices_host[5] = {0, 3, 4, 7, 9};
math_t output_exp_colmajor[15] = {0, 3, 4, 7, 9, 10, 13, 14, 17, 19, 20, 23, 24, 27, 29};
math_t output_exp_rowmajor[15] = {0, 1, 2, 9, 10, 11, 12, 13, 14, 21, 22, 23, 27, 28, 29};
rmm::device_uvector<math_t> input;
rmm::device_uvector<math_t> output;
rmm::device_uvector<idx_array_t> indices;
};
using TypeTuple = ::testing::Types<std::tuple<float, int, int>,
std::tuple<float, int64_t, int>,
std::tuple<double, int, int>,
std::tuple<double, int64_t, int>>;
TYPED_TEST_CASE(MatrixCopyRowsTest, TypeTuple);
TYPED_TEST(MatrixCopyRowsTest, CopyRows) { this->testCopyRows(); }
} // namespace matrix
} // namespace raft
|
2b375ca1064061439fdc87fb32d664cc9434d26e.hip | "// !!! This is a file automatically generated by hipify!!!\n#include <stdio.h>\n#include <stdint.h>(...TRUNCATED) | 2b375ca1064061439fdc87fb32d664cc9434d26e.cu | "#include <stdio.h>\n#include <stdint.h>\n#include <stdlib.h>\n#include <cuda_runtime.h>\n#include \(...TRUNCATED) |
7585ae37db845c3d72867067b861aa6c2cf3e14f.hip | "// !!! This is a file automatically generated by hipify!!!\n#include \"hip/hip_runtime.h\"\n/* ****(...TRUNCATED) | 7585ae37db845c3d72867067b861aa6c2cf3e14f.cu | "/* ******************************************************************************\n *\n *\n * This (...TRUNCATED) |
c3188cd3f334ac6d986d1f375d9a8801292d5b80.hip | "// !!! This is a file automatically generated by hipify!!!\n#include \"hip/hip_runtime.h\"\n#includ(...TRUNCATED) | c3188cd3f334ac6d986d1f375d9a8801292d5b80.cu | "#include \"poisson2d.hpp\"\n#include \"timer.hpp\"\n#include <algorithm>\n#include <cmath>\n#includ(...TRUNCATED) |
96bb668fecc8b0a34b7650aef53cc31e3a01ee28.hip | "// !!! This is a file automatically generated by hipify!!!\n#include <stdbool.h>\n#include <stdio.h(...TRUNCATED) | 96bb668fecc8b0a34b7650aef53cc31e3a01ee28.cu | "#include <stdbool.h>\n#include <stdio.h>\n#include <string.h>\n#include <getopt.h>\n#include <curan(...TRUNCATED) |
9dc24335940e31d15412f59d6fc8dc41080fd2fa.hip | "// !!! This is a file automatically generated by hipify!!!\n#include \"hip/hip_runtime.h\"\n// Home(...TRUNCATED) | 9dc24335940e31d15412f59d6fc8dc41080fd2fa.cu | "// Homework 1\n// Color to Greyscale Conversion\n\n//A common way to represent color images is know(...TRUNCATED) |
fdde141701ec6aef7c1c0271fc242e3b55fa093c.hip | "// !!! This is a file automatically generated by hipify!!!\n#define TORCH_ASSERT_NO_OPERATORS\n#inc(...TRUNCATED) | fdde141701ec6aef7c1c0271fc242e3b55fa093c.cu | "#define TORCH_ASSERT_NO_OPERATORS\n#include <ATen/AccumulateType.h>\n#include <ATen/Dispatch.h>\n#i(...TRUNCATED) |
End of preview. Expand
in Dataset Viewer.
README.md exists but content is empty.
- Downloads last month
- 25