- Timestamp:
- 09/25/17 19:20:08 (7 years ago)
- Branches:
- master, pympi
- Children:
- 56b997e
- Parents:
- b02d091
- git-author:
- Hal Finkel <hfinkel@…> (09/25/17 19:20:08)
- git-committer:
- Hal Finkel <hfinkel@…> (09/25/17 19:20:08)
- Location:
- python
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
python/example.py
rc13d973 r9796d0a 39 39 40 40 import sys 41 import numpy as np 41 42 import genericio as gio 42 43 … … 44 45 gio.gio_inspect(name) 45 46 46 x = gio.gio_read(name, "x") 47 print x 47 if gio.gio_has_variable(name, "x"): 48 x = gio.gio_read(name, "x") 49 y = gio.gio_read(name, "y") 50 z = gio.gio_read(name, "z") 51 print np.column_stack((x, y, z)) 52 else: 53 pos = gio.gio_read(name, "pos") 54 print pos 48 55 -
python/genericio.py
rc13d973 r9796d0a 53 53 libpygio.get_variable_type.argtypes=[ct.c_char_p,ct.c_char_p] 54 54 55 libpygio.get_variable_field_count.restype=ct.c_int 56 libpygio.get_variable_field_count.argtypes=[ct.c_char_p,ct.c_char_p] 57 55 58 libpygio.read_gio_int32.restype=None 56 libpygio.read_gio_int32.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int) ]59 libpygio.read_gio_int32.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int),ct.c_int] 57 60 58 61 libpygio.read_gio_int64.restype=None 59 libpygio.read_gio_int64.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int64) ]62 libpygio.read_gio_int64.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int64),ct.c_int] 60 63 61 64 libpygio.read_gio_float.restype=None 62 libpygio.read_gio_float.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_float) ]65 libpygio.read_gio_float.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_float),ct.c_int] 63 66 64 67 libpygio.read_gio_double.restype=None 65 libpygio.read_gio_double.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_double) ]68 libpygio.read_gio_double.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_double),ct.c_int] 66 69 67 70 libpygio.inspect_gio.restype=None … … 71 74 var_size = libpygio.get_elem_num(file_name) 72 75 var_type = libpygio.get_variable_type(file_name,var_name) 76 field_count = libpygio.get_variable_field_count(file_name,var_name) 73 77 if(var_type==10): 74 78 print "Variable not found" … … 78 82 elif(var_type==0): 79 83 #float 80 result = np.ndarray( var_size,dtype=np.float32)81 libpygio.read_gio_float(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_float)) )84 result = np.ndarray((var_size,field_count),dtype=np.float32) 85 libpygio.read_gio_float(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_float)),field_count) 82 86 return result 83 87 elif(var_type==1): 84 88 #double 85 result = np.ndarray( var_size,dtype=np.float64)86 libpygio.read_gio_double(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_double)) )89 result = np.ndarray((var_size,field_count),dtype=np.float64) 90 libpygio.read_gio_double(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_double)),field_count) 87 91 return result 88 92 elif(var_type==2): 89 93 #int32 90 result = np.ndarray( var_size,dtype=np.int32)91 libpygio.read_gio_int32(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int32)) )94 result = np.ndarray((var_size,field_count),dtype=np.int32) 95 libpygio.read_gio_int32(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int32)),field_count) 92 96 return result 93 97 elif(var_type==3): 94 98 #int64 95 result = np.ndarray( var_size,dtype=np.int64)96 libpygio.read_gio_int64(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int64)) )99 result = np.ndarray((var_size,field_count),dtype=np.int64) 100 libpygio.read_gio_int64(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int64)),field_count) 97 101 return result 98 102 103 def gio_has_variable(file_name,var_name): 104 var_size = libpygio.get_elem_num(file_name) 105 var_type = libpygio.get_variable_type(file_name,var_name) 106 return var_type!=10 107 99 108 def gio_inspect(file_name): 100 109 libpygio.inspect_gio(file_name) -
python/lib/gio.cxx
rbd84570 r9796d0a 43 43 #include <iostream> 44 44 45 void read_gio_float(char* file_name, char* var_name, float* data ){46 read_gio<float>(file_name,var_name,data );45 void read_gio_float(char* file_name, char* var_name, float* data, int field_count){ 46 read_gio<float>(file_name,var_name,data,field_count); 47 47 } 48 void read_gio_double(char* file_name, char* var_name, double* data ){49 read_gio<double>(file_name,var_name,data );48 void read_gio_double(char* file_name, char* var_name, double* data, int field_count){ 49 read_gio<double>(file_name,var_name,data,field_count); 50 50 } 51 void read_gio_int32(char* file_name, char* var_name, int* data ){52 read_gio<int>(file_name,var_name,data );51 void read_gio_int32(char* file_name, char* var_name, int* data, int field_count){ 52 read_gio<int>(file_name,var_name,data,field_count); 53 53 } 54 void read_gio_int64(char* file_name, char* var_name, int64_t* data ){55 read_gio<int64_t>(file_name,var_name,data );54 void read_gio_int64(char* file_name, char* var_name, int64_t* data, int field_count){ 55 read_gio<int64_t>(file_name,var_name,data,field_count); 56 56 } 57 57 … … 77 77 gio::GenericIO::VariableInfo vinfo = VI[i]; 78 78 if(vinfo.Name == var_name){ 79 if(vinfo.IsFloat && vinfo. Size == 4)79 if(vinfo.IsFloat && vinfo.ElementSize == 4) 80 80 return float_type; 81 else if(vinfo.IsFloat && vinfo. Size == 8)81 else if(vinfo.IsFloat && vinfo.ElementSize == 8) 82 82 return double_type; 83 else if(!vinfo.IsFloat && vinfo. Size == 4)83 else if(!vinfo.IsFloat && vinfo.ElementSize == 4) 84 84 return int32_type; 85 else if(!vinfo.IsFloat && vinfo. Size == 8)85 else if(!vinfo.IsFloat && vinfo.ElementSize == 8) 86 86 return int64_type; 87 87 else … … 91 91 return var_not_found; 92 92 93 } 94 95 int get_variable_field_count(char* file_name,char* var_name){ 96 gio::GenericIO reader(file_name); 97 std::vector<gio::GenericIO::VariableInfo> VI; 98 reader.openAndReadHeader(gio::GenericIO::MismatchAllowed); 99 reader.getVariableInfo(VI); 100 101 int num =VI.size(); 102 for(int i =0;i<num;++i){ 103 gio::GenericIO::VariableInfo vinfo = VI[i]; 104 if(vinfo.Name == var_name) { 105 return vinfo.Size/vinfo.ElementSize; 106 } 107 } 108 return 0; 93 109 } 94 110 … … 110 126 else 111 127 std::cout<<"[i"; 112 std::cout<<" "<<vinfo.Size*8<<"] "; 128 int NumElements = vinfo.Size/vinfo.ElementSize; 129 std::cout<<" "<<vinfo.ElementSize*8; 130 if (NumElements > 1) 131 std::cout<<"x"<<NumElements; 132 std::cout<<"] "; 113 133 std::cout<<vinfo.Name<<std::endl; 114 134 } -
python/lib/gio.h
rbd84570 r9796d0a 46 46 47 47 template <class T> 48 void read_gio(char* file_name, std::string var_name, T*& data ){48 void read_gio(char* file_name, std::string var_name, T*& data, int field_count){ 49 49 gio::GenericIO reader(file_name); 50 50 reader.openAndReadHeader(gio::GenericIO::MismatchAllowed); … … 57 57 max_size = rank_size[i]; 58 58 } 59 T* rank_data = new T[max_size +reader.requestedExtraSpace()/sizeof(T)];59 T* rank_data = new T[max_size*field_count+reader.requestedExtraSpace()/sizeof(T)]; 60 60 int64_t offset =0; 61 reader.addVariable(var_name,rank_data,true); 61 reader.addScalarizedVariable(var_name,rank_data,field_count, 62 gio::GenericIO::VarHasExtraSpace); 62 63 for(int i=0;i<num_ranks;++i){ 63 64 reader.readData(i,false); 64 std::copy(rank_data,rank_data+rank_size[i] ,data+offset);65 offset +=rank_size[i] ;65 std::copy(rank_data,rank_data+rank_size[i]*field_count,data+offset); 66 offset +=rank_size[i]*field_count; 66 67 } 67 68 delete [] rank_data; … … 70 71 extern "C" int64_t get_elem_num(char* file_name); 71 72 72 extern "C" void read_gio_float (char* file_name, char* var_name, float* data );73 extern "C" void read_gio_double(char* file_name, char* var_name, double* data );74 extern "C" void read_gio_int32 (char* file_name, char* var_name, int* data );75 extern "C" void read_gio_int64 (char* file_name, char* var_name, int64_t* data );73 extern "C" void read_gio_float (char* file_name, char* var_name, float* data, int field_count); 74 extern "C" void read_gio_double(char* file_name, char* var_name, double* data, int field_count); 75 extern "C" void read_gio_int32 (char* file_name, char* var_name, int* data, int field_count); 76 extern "C" void read_gio_int64 (char* file_name, char* var_name, int64_t* data, int field_count); 76 77 enum var_type{ 77 78 float_type=0, … … 83 84 }; 84 85 extern "C" var_type get_variable_type(char* file_name,char* var_name); 86 extern "C" int get_variable_field_count(char* file_name,char* var_name); 85 87 extern "C" void inspect_gio(char* file_name);
Note: See TracChangeset
for help on using the changeset viewer.