/*
* ReadCSV.h
* Created on: Nov 27, 2017
* Author: zlf
*/
#ifndef IMAGE_MTF_H_
#define IMAGE_MTF_H_
void my_ReadSingleCSV(char *path, double **arr2, int ROWS, int COLS);
void my_ReadMultiCSV(char ** paths, double ***arr3D, int NUMS, int ROWS, int COLS);
double *** my_ReadingMultiCSV(int NUMS, int ROWS, int COLS);
#endif
/*
* ReadCSV.cu
* */
#include <stdlib.h>
#include<stdio.h>
#include <iostream>
using namespace std;
#include "./ReadCSV.h"
//=================================================================================
//path: the path of CSV file
//arr2: the data of CSV saved in arr2
//the memory of arr2 is not malloc()ed in my_ReadSingleCSV(),
//thus,the function that call my_ReadSingleCSV() must malloc() memory for arr2
//ROWS: total rows of CSV sheets
//COLS: total columns of CSV sheets
void my_ReadSingleCSV(char *path, double **arr2, int ROWS, int COLS)
{
FILE *fp = NULL;
fp = fopen(path, "at+");
if ( NULL == fp )
{
cout << "Failed to open " << path << endl;
}
char d;
int ret=1;
int i = 0;
while ( ret != EOF && i < ROWS )
{
for(int j=0; j<COLS; j++)
{
ret = fscanf( fp, "%lf", &arr2[i][j]);
ret = fscanf( fp, ",", &d);
}
i++;
}
fclose(fp);
}
//=================================================================================
//paths: the path of all CSV sheets in paths
//arr3D: the data of all CSV sheets saved in arr3D
//the memory of arr3D is not malloc()ed in my_ReadMultiCSV(),
//thus,the function that call my_ReadSingleCSV() must malloc() memory for arr3D
//NUMS: the number of CSV sheets to be read
//ROWS: total rows of every CSV sheets
//COLS: total columns of every CSV sheets
void my_ReadMultiCSV(char ** paths, double ***arr3D, int NUMS, int ROWS, int COLS)
{
for(int n=0; n<NUMS; n++)
{
char path[260];
strcpy(path, paths[n]);
my_ReadSingleCSV(path, arr3D[n], ROWS, COLS);
}
}
//=================================================================================
//we have malloc() contiguous memory chunks for arr3D in my_ReadingMultiCSV()
//thus, in function that call my_ReadingMultiCSV(), we just do
//double ***DatasAllCSV = my_ReadingMultiCSV(NUMS, ROWS, COLS);
double *** my_ReadingMultiCSV(int NUMS, int ROWS, int COLS)
{
//*------------------------ set paths ------------------------------*//
char *paths1D;
paths1D = (char *)malloc( NUMS*260*sizeof(char) );
char **paths2D;
paths2D = (char **)malloc(NUMS*sizeof(char *));
for(int n=0; n<NUMS; n++)
{
paths2D[n] = paths1D + n*260;
}
strcpy(paths2D[0], "/home/zlf/Documents/code/MTF/MTF_rdc.csv");
strcpy(paths2D[1], "/home/zlf/Documents/code/MTF/MTF_gdc.csv");
strcpy(paths2D[2], "/home/zlf/Documents/code/MTF/MTF_bdc.csv");
strcpy(paths2D[3], "/home/zlf/Documents/code/MTF/MTF_rac.csv");
//*------------------------ set arr3D ------------------------------*//
double * arr1D=NULL;
arr1D = (double *)malloc( NUMS*ROWS*COLS*sizeof(double) );
double ** arr2D=NULL;
arr2D = (double **)malloc(NUMS*ROWS*sizeof(double *));
for(int dh=0; dh<NUMS*ROWS; dh++)
{
arr2D[dh] = arr1D + dh*COLS;
}
double *** arr3D=NULL;
arr3D = (double ***)malloc(NUMS*sizeof(double **));
for(int d=0; d<NUMS; d++)
{
arr3D[d] = arr2D +d*ROWS;
}
my_ReadMultiCSV(paths2D, arr3D, NUMS, ROWS, COLS);
return arr3D;
}
#include <stdio.h>
#include <cuda_runtime.h>
#include "./ReadCSV.h"
__global__ void CSV2CUDA(cudaPitchedPtr devPitchedPtr, cudaExtent extent )
{
printf("IN CSV2CUDA \n");
char* devPtr = (char*)devPitchedPtr.ptr;
size_t pitch= devPitchedPtr.pitch;
size_t slicePitch = pitch * extent.height;
printf("pitch=%d ", devPitchedPtr.pitch);
printf("extent.width=%d ", extent.width);
printf("extent.height=%d ", extent.height);
printf("extent.depth=%d \n", extent.depth);
for(int k=0; k <extent.depth; k++)
{
char* slice = devPtr + k * slicePitch;
for(int j=0; j<extent.height; j++)
{
double* row = (double*)(slice+j*pitch);
for (int i=0; i<extent.width/sizeof(double); i++)
{
printf("%f ", row[i]);
}
printf("\n");
}
printf("\n");
}
printf("OUT CSV2CUDA \n");
}
int main()
{
int Depth=4;
int Height=2;
int Width=2;
double bmp2[Depth][Height][Width];
double *bmp1=NULL;
double ***arr3D=NULL;
arr3D = my_ReadingMultiCSV(Depth, Height, Width);
bmp1 = arr3D[0][0];
for(int k=0; k<4; k++)
{
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
//bmp1[k][i][j]=arr3[k][i][j];
printf("%f ", arr3D[k][i][j]);
}
printf("\n");
}
printf("\n\n");
}
cudaPitchedPtr devPitchedPtr;
cudaExtent extent = make_cudaExtent(sizeof(double)*2, 2, 4);
cudaMalloc3D(&devPitchedPtr, extent);
cudaMemset3D( devPitchedPtr, 0, extent);
cudaError status;
cudaMemcpy3DParms HostToDev = { 0 };
HostToDev.srcPtr = make_cudaPitchedPtr((void*)bmp1, sizeof(double)*2, 2, 2);
HostToDev.dstPtr = devPitchedPtr;
HostToDev.extent = extent;
HostToDev.kind = cudaMemcpyHostToDevice;
status = cudaMemcpy3D(&HostToDev);
if(status != cudaSuccess){
fprintf(stderr, "MemcpyHtD: %s\n", cudaGetErrorString(status));
}
CSV2CUDA<<<1,1>>>(devPitchedPtr, extent);
cudaMemcpy3DParms DevToHost = {0};
DevToHost.srcPtr = devPitchedPtr;
DevToHost.dstPtr = make_cudaPitchedPtr((void*)bmp2, sizeof(double)*2, 2, 2);
DevToHost.extent = extent;
DevToHost.kind = cudaMemcpyDeviceToHost;
status = cudaMemcpy3D(&DevToHost);
if(status != cudaSuccess){
fprintf(stderr, "MemcpyHtD: %s\n", cudaGetErrorString(status));
}
cudaFree(&devPitchedPtr);
for(int i=0; i<Depth; i++)
{
printf("bmp2[%d]=: \n", i);
for(int j=0; j<Height; j++)
{
for(int k=0; k<Width; k++)
{
printf("%f ", i, j, k, bmp2[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}