Skip to content

Commit

Permalink
Adding additional input output utility functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Chancellor Pascale authored and Chancellor Pascale committed Jul 14, 2017
1 parent de17061 commit 545449f
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
70 changes: 70 additions & 0 deletions common/inc/InputOutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <fstream>
#include <iomanip>
#include <stdlib.h>

#include "InputOutput.h"

#define prec_save 15

/***********************************************/
/* SAVE INDIVIDUAL REAL CPU MATRIX TO txt FILE */
/***********************************************/
template <class T>
void saveCPUrealtxt(const T * h_in, const char *filename, const int M) {

std::ofstream outfile;
outfile.open(filename);
for (int i = 0; i < M; i++) outfile << std::setprecision(prec_save) << h_in[i] << "\n";
outfile.close();

}

template void saveCPUrealtxt<float>(const float *, const char *, const int);
template void saveCPUrealtxt<double>(const double *, const char *, const int);

/**************************************************/
/* SAVE INDIVIDUAL INTEGER CPU MATRIX TO txt FILE */
/**************************************************/
template <class T>
void saveCPUintegertxt(const T * h_in, const char *filename, const int M) {

std::ofstream outfile;
outfile.open(filename);
for (int i = 0; i < M; i++) outfile << h_in[i] << "\n";
outfile.close();

}

template void saveCPUintegertxt<int>(const int *, const char *, const int);

/****************************************************/
/* LOAD INDIVIDUAL REAL MATRIX FROM txt FILE TO CPU */
/****************************************************/
// --- Load individual real matrix from txt file
template <class T>
void loadCPUrealtxt(T * __restrict h_out, const char *filename, const int M) {
//T * loadCPUrealtxt(T * __restrict h_out, const char *filename, const int M) {

//h_out = (T *)malloc(M * sizeof(T));

std::ifstream infile;
infile.open(filename);
for (int i = 0; i < M; i++) {
double temp;
infile >> temp;
h_out[i] = (T)temp;
}

infile.close();

//return h_out;

}

template void loadCPUrealtxt<int>(int * __restrict, const char *, const int);
template void loadCPUrealtxt<float>(float * __restrict, const char *, const int);
template void loadCPUrealtxt<double>(double * __restrict, const char *, const int);
//template float * loadCPUrealtxt<float>(float * __restrict, const char *, const int);
//template double * loadCPUrealtxt<double>(double * __restrict, const char *, const int);


148 changes: 148 additions & 0 deletions common/inc/InputOutput.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <fstream>
#include <iomanip>

#include <cuda.h>

#include "InputOutput.cuh"
#include "BBComplex.h"
#include "Utilities.cuh"

#define prec_save 10

/***********************************************/
/* SAVE INDIVIDUAL REAL GPU MATRIX TO txt FILE */
/***********************************************/
template <class T>
void saveGPUrealtxt(const T * d_in, const char *filename, const int M) {

T *h_in = (T *)malloc(M * sizeof(T));

gpuErrchk(cudaMemcpy(h_in, d_in, M * sizeof(T), cudaMemcpyDeviceToHost));

std::ofstream outfile;
outfile.open(filename);
for(int i = 0; i < M; i++) outfile << std::setprecision(prec_save) << h_in[i] << "\n";
outfile.close();

}

template void saveGPUrealtxt<float> (const float *, const char *, const int);
template void saveGPUrealtxt<double>(const double *, const char *, const int);

/***********************************************/
/* SAVE INDIVIDUAL REAL CPU MATRIX TO txt FILE */
/***********************************************/
template <class T>
void saveCPUrealtxt(const T * h_in, const char *filename, const int M) {

std::ofstream outfile;
outfile.open(filename);
for(int i = 0; i < M; i++) outfile << std::setprecision(prec_save) << h_in[i] << "\n";
outfile.close();

}

template void saveCPUrealtxt<float> (const float *, const char *, const int);
template void saveCPUrealtxt<double>(const double *, const char *, const int);

/**************************************************/
/* SAVE INDIVIDUAL COMPLEX GPU MATRIX TO txt FILE */
/**************************************************/
template <class T>
void saveGPUcomplextxt(const T * d_in, const char *filename, const int M) {

T *h_in = (T *)malloc(M * sizeof(T));

gpuErrchk(cudaMemcpy(h_in, d_in, M * sizeof(T), cudaMemcpyDeviceToHost));

std::ofstream outfile;
outfile.open(filename);
for(int i = 0; i < M; i++) {
//printf("%f %f\n", h_in[i].c.x, h_in[i].c.y);
outfile << std::setprecision(prec_save) << h_in[i].c.x << "\n"; outfile << std::setprecision(prec_save) << h_in[i].c.y << "\n";
}
outfile.close();

}

void saveGPUcomplextxt(const float2 * d_in, const char *filename, const int M) {

float2 *h_in = (float2 *)malloc(M * sizeof(float2));

gpuErrchk(cudaMemcpy(h_in, d_in, M * sizeof(float2), cudaMemcpyDeviceToHost));

std::ofstream outfile;
outfile.open(filename);
for(int i = 0; i < M; i++) {
//printf("%f %f\n", h_in[i].c.x, h_in[i].c.y);
outfile << std::setprecision(prec_save) << h_in[i].x << "\n"; outfile << std::setprecision(prec_save) << h_in[i].y << "\n";
}
outfile.close();

}

void saveGPUcomplextxt(const double2 * d_in, const char *filename, const int M) {

double2 *h_in = (double2 *)malloc(M * sizeof(double2));

gpuErrchk(cudaMemcpy(h_in, d_in, M * sizeof(double2), cudaMemcpyDeviceToHost));

std::ofstream outfile;
outfile.open(filename);
for(int i = 0; i < M; i++) {
//printf("%f %f\n", h_in[i].c.x, h_in[i].c.y);
outfile << std::setprecision(prec_save) << h_in[i].x << "\n"; outfile << std::setprecision(prec_save) << h_in[i].y << "\n";
}
outfile.close();

}

template void saveGPUcomplextxt<float2_> (const float2_ *, const char *, const int);
template void saveGPUcomplextxt<double2_>(const double2_ *, const char *, const int);

/****************************************************/
/* LOAD INDIVIDUAL REAL MATRIX FROM txt FILE TO CPU */
/****************************************************/
// --- Load individual real matrix from txt file
template <class T>
T * loadCPUrealtxt(const char *filename, T * __restrict__ h_out, const int M) {

h_out = (T *)malloc(M * sizeof(T));

std::ifstream infile;
infile.open(filename);
for(int i = 0; i < M; i++) infile >> h_out[i];
infile.close();

return h_out;

}

template float * loadCPUrealtxt<float> (const char *, float * __restrict__, const int);
template double * loadCPUrealtxt<double>(const char *, double * __restrict__, const int);

/****************************************************/
/* LOAD INDIVIDUAL REAL MATRIX FROM txt FILE TO GPU */
/****************************************************/
// --- Load individual real matrix from txt file
template <class T>
T * loadGPUrealtxt(const char *filename, T * __restrict__ d_out, const int M) {

T *h_out = (T *)malloc(M * sizeof(T));
//T *d_out; gpuErrchk(cudaMalloc(&d_out, M * sizeof(T)));
gpuErrchk(cudaMalloc((void**)&d_out, M * sizeof(T)));

std::ifstream infile;
infile.open(filename);
for(int i = 0; i < M; i++) infile >> h_out[i];
infile.close();

gpuErrchk(cudaMemcpy(d_out, h_out, M * sizeof(T), cudaMemcpyHostToDevice));

return d_out;

}

template float * loadGPUrealtxt<float> (const char *, float * __restrict__, const int);
template double * loadGPUrealtxt<double>(const char *, double * __restrict__, const int);

21 changes: 21 additions & 0 deletions common/inc/InputOutput.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef INPUTOUTPUT_CUH
#define INPUTOUTPUT_CUH

template <class T>
void saveGPUrealtxt(const T *, const char *, const int);

template <class T>
void saveCPUrealtxt(const T *, const char *, const int);

template <class T>
void saveGPUcomplextxt(const T *, const char *, const int);

void saveGPUcomplextxt(const double2 *, const char *, const int);

template <class T>
T * loadCPUrealtxt(const char *, T * __restrict__, const int);

template <class T>
T * loadGPUrealtxt(const char *, T * __restrict__, const int);

#endif
14 changes: 14 additions & 0 deletions common/inc/InputOutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef INPUTOUTPUT_H
#define INPUTOUTPUT_H

template <class T>
void saveCPUrealtxt(const T *, const char *, const int);

template <class T>
void saveCPUintegertxt(const T *, const char *, const int);

template <class T>
void loadCPUrealtxt(T * __restrict, const char *, const int);
//T * loadCPUrealtxt(T * __restrict, const char *, const int);

#endif

0 comments on commit 545449f

Please sign in to comment.