00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __PxReadWriteRaw_h_
00011 #define __PxReadWriteRaw_h_
00012
00013
00014 #include <vector>
00015 #include "Basis/String.h"
00016 #include "Core/Array/Element/TypeString.h"
00017 #include "Basis/NativeTypeRead.h"
00018 #include "Basis/NativeTypeFormats.h"
00019 #include "Core/Array/ArrayType.h"
00020 #include "Core/Array/Pattern/ArrayFunc.h"
00021
00022 #include "Core/Array/Pattern/PxSystem.h"
00023 #include "mpi.h"
00024
00025 namespace Impala
00026 {
00027 namespace Core
00028 {
00029 namespace Array
00030 {
00031 namespace Pattern
00032 {
00033
00034
00035 template<class ArrayT>
00036 inline ArrayT*
00037 PxReadRaw(ArrayT* dst, std::string fileName)
00038 {
00039 typedef typename ArrayT::StorType StorT;
00040
00041 FILE* fp;
00042
00043 if (PxMyCPU() == PxRootCPU()) {
00044 if (! (fp = fopen(fileName.c_str(), "r"))) {
00045 PX_COUT << "PxReadRaw: unable to open file " << fileName
00046 << PX_ENDL;
00047 PxAbortSystem();
00048 }
00049 }
00050
00051 std::string typeStr = Element::TypeString<StorT>(0);
00052 std::string formatStr = std::string("version: 1, binary: %d, type: ")
00053 + typeStr
00054 + std::string(", elemSize: %d, width: %d, height: %d, nr: %d");
00055 int binary;
00056 int elemSize;
00057 int width;
00058 int height;
00059 int nrA;
00060 char buf[200];
00061
00062 if (PxMyCPU() == PxRootCPU()) {
00063 fgets(buf, 200, fp);
00064 int res = sscanf(buf, formatStr.c_str(),
00065 &binary, &elemSize, &width, &height, &nrA);
00066 if ((res != 5) ||
00067 (elemSize != ArrayT::ElemSize()) || (nrA != 1)) {
00068 fclose(fp);
00069 PX_COUT << "Header of " << fileName << " is not compatible"
00070 << PX_ENDL;
00071 PxAbortSystem();
00072 }
00073 }
00074 int blockSize = width*height*elemSize;
00075
00076 int sizes[2];
00077 sizes[0] = width;
00078 sizes[1] = height;
00079 MPI_Bcast(sizes, 2, MPI_INT, PxRootCPU(), MPI_COMM_WORLD);
00080
00081 if (dst == 0)
00082 dst = ArrayCreate<ArrayT>(sizes[0], sizes[1]);
00083
00084 if (PxMyCPU() == PxRootCPU()) {
00085 StorT* dstPtr = ArrayCPB(dst, 0, 0);
00086 if (binary) {
00087 fclose(fp);
00088 fp = fopen(fileName.c_str(), "rb");
00089 fread(buf, sizeof(char), 200, fp);
00090 for (int n=0 ; n<nrA ; n++) {
00091 fread(dstPtr, sizeof(StorT), blockSize, fp);
00092 dstPtr += blockSize;
00093 }
00094 } else {
00095 for (int n=0 ; n<nrA ; n++) {
00096 for(int i=0 ; i<height ; i++) {
00097 for(int j=0 ; j<width ; j++) {
00098 for (int k=0 ; k<elemSize ; k++) {
00099 NativeTypeRead(fp, dstPtr);
00100 dstPtr++;
00101 }
00102 }
00103 }
00104 }
00105 }
00106 fclose(fp);
00107 }
00108
00109 PxBcastArray(dst, PxRootCPU());
00110 return dst;
00111 }
00112
00113
00114 template<class ArrayT>
00115 inline void
00116 PxWriteRaw(ArrayT* src, std::string fileName, bool binary)
00117 {
00118 PxArrayForceNonDistributed(src);
00119
00120 if (PxMyCPU() == PxRootCPU()) {
00121 typedef typename ArrayT::StorType StorT;
00122
00123 std::string typeStr = Element::TypeString<StorT>(0);
00124 std::string formatStr = std::string("version: 1, binary: %d, type: ")
00125 + typeStr + std::string(", elemSize: %d, width: %d, height: %d, nr: %d");
00126 int width = ArrayCW(src);
00127 int height = ArrayCH(src);
00128 int elemSize = ArrayT::ElemSize();
00129 int nrA = 1;
00130 char buf[200];
00131 sprintf(buf, formatStr.c_str(), binary, elemSize, width, height, nrA);
00132
00133 FILE* fp;
00134 if (binary) {
00135 fp = fopen(fileName.c_str(), "wb");
00136 fwrite(buf, sizeof(char), 200, fp);
00137 for (int y=0 ; y<height ; y++) {
00138 StorT* srcPtr = ArrayCPB(src, 0, y);
00139 fwrite(srcPtr, sizeof(StorT), width*elemSize, fp);
00140 }
00141 } else {
00142 fp = fopen(fileName.c_str(), "w") ;
00143 fprintf(fp, "%s\n", buf);
00144 std::string fs = NativeTypeFormat<StorT>(0) + std::string(" ");
00145 for (int n=0 ; n<nrA ; n++) {
00146 for(int i=0 ; i<height ; i++) {
00147 StorT* srcPtr = ArrayCPB(src, 0, i);
00148 for(int j=0 ; j<width ; j++) {
00149 for (int k=0 ; k<elemSize ; k++) {
00150 fprintf(fp, fs.c_str(), *srcPtr++);
00151 }
00152 }
00153 fprintf(fp, "\n");
00154 }
00155 }
00156 }
00157 fclose(fp);
00158 }
00159 }
00160
00161
00162 }
00163 }
00164 }
00165 }
00166
00167 #endif