testing updates
parent
4e0a826903
commit
94060cee88
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef __AMSCPPNARRAY_BINRW_HPP__
|
||||||
|
#define __AMSCPPNARRAY_BINRW_HPP__
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
//binary array read/write operations
|
||||||
|
|
||||||
|
template<typename T> int fwrite_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape);
|
||||||
|
|
||||||
|
template<typename T> int fread_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape);
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <amscppnarray/amscppnarray_binrwimpl.hpp>
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,153 @@
|
|||||||
|
#ifndef __AMSCPPNARRAY_BINRWIMPL_HPP__
|
||||||
|
#define __AMSCPPNARRAY_BINRWIMPL_HPP__
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
//binary array read/write operations
|
||||||
|
|
||||||
|
template<typename T> int fwrite_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape)
|
||||||
|
{
|
||||||
|
int ret = narray_success;
|
||||||
|
narray_size_t piprod;
|
||||||
|
narray_size_t I;
|
||||||
|
int32_t Nd;
|
||||||
|
narray<int32_t> *lshape=NULL;
|
||||||
|
|
||||||
|
if(fp==NULL)
|
||||||
|
{
|
||||||
|
ret = narray_failure;
|
||||||
|
printf("fwrite_ndarray: fp=NULL\n");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shape==NULL)
|
||||||
|
{
|
||||||
|
lshape = new(std::nothrow) narray<int32_t>();
|
||||||
|
lshape->resize(1);
|
||||||
|
lshape->data[0] = data->length;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lshape = shape;
|
||||||
|
}
|
||||||
|
|
||||||
|
piprod = 1;
|
||||||
|
for(I=0;I<lshape->length;I++)
|
||||||
|
{
|
||||||
|
if(lshape->data[I]>0)
|
||||||
|
{
|
||||||
|
piprod = piprod*lshape->data[I];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
piprod = 0; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(piprod!=data->length)
|
||||||
|
{
|
||||||
|
ret = narray_failure;
|
||||||
|
printf("fwrite_ndarray: buffer is size %ld, while shape is size %ld\n",(long) data->length,(long) piprod);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
Nd = lshape->length;
|
||||||
|
|
||||||
|
fwrite(&Nd,sizeof(int32_t),1,fp);
|
||||||
|
if(Nd>0)
|
||||||
|
{
|
||||||
|
fwrite(lshape->data,sizeof(int32_t),Nd,fp);
|
||||||
|
if(piprod>0)
|
||||||
|
{
|
||||||
|
fwrite(data->data,sizeof(T),data->length,fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shape==NULL)
|
||||||
|
{
|
||||||
|
if(lshape!=NULL) {delete lshape; lshape=NULL;}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> int fread_ndarray(FILE *fp, narray<T> *data, narray<int32_t> *shape)
|
||||||
|
{
|
||||||
|
int ret = narray_success;
|
||||||
|
narray_size_t piprod;
|
||||||
|
narray_size_t I;
|
||||||
|
int32_t Nd;
|
||||||
|
narray<int32_t> *lshape=NULL;
|
||||||
|
|
||||||
|
if(fp==NULL)
|
||||||
|
{
|
||||||
|
ret = narray_failure;
|
||||||
|
printf("fread_ndarray: fp=NULL\n");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shape==NULL)
|
||||||
|
{
|
||||||
|
lshape = new(std::nothrow) narray<int32_t>();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lshape = shape;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fread(&Nd,sizeof(int32_t),1,fp);
|
||||||
|
if(Nd<=0)
|
||||||
|
{
|
||||||
|
data->resize(0);
|
||||||
|
lshape->resize(0);
|
||||||
|
ret = narray_failure;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
lshape->resize(Nd);
|
||||||
|
fread(lshape->data,sizeof(int32_t),Nd,fp);
|
||||||
|
|
||||||
|
piprod = 1;
|
||||||
|
for(I=0;I<lshape->length;I++)
|
||||||
|
{
|
||||||
|
if(lshape->data[I]>0)
|
||||||
|
{
|
||||||
|
piprod = piprod*lshape->data[I];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
piprod = 0; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(piprod<=0)
|
||||||
|
{
|
||||||
|
data->resize(0);
|
||||||
|
lshape->resize(0);
|
||||||
|
ret = narray_failure;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
data->resize(piprod);
|
||||||
|
fread(data->data,sizeof(T),data->length,fp);
|
||||||
|
|
||||||
|
if(shape==NULL)
|
||||||
|
{
|
||||||
|
if(lshape!=NULL) {delete lshape; lshape=NULL;}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef __AMSCPPNARRAY_LLBOPS_HPP__
|
||||||
|
#define __AMSCPPNARRAY_LLBOPS_HPP__
|
||||||
|
|
||||||
|
//Low level threaded buffer operations
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T1, typename T2> void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N);
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T> void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <amscppnarray/amscppnarray_llbopsimpl.hpp>
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,166 @@
|
|||||||
|
#ifndef __AMSCPPNARRAY_LLBOPSIMPL_HPP__
|
||||||
|
#define __AMSCPPNARRAY_LLBOPSIMPL_HPP__
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace narray
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T1, typename T2> void buffer_copycast_tf(
|
||||||
|
T1 *bufferto,
|
||||||
|
const T2 *bufferfrom,
|
||||||
|
const narray_size_t N,
|
||||||
|
int threadnum, int nthreads
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int I0,I1,Is,I;
|
||||||
|
|
||||||
|
Is = N/nthreads;
|
||||||
|
I0 = Is*threadnum;
|
||||||
|
I1 = (threadnum>=(nthreads-1))? N: Is*(threadnum+1);
|
||||||
|
for(I=I0;I<I1;I++)
|
||||||
|
{
|
||||||
|
bufferto[I] = (T1) bufferfrom[I];
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T1, typename T2> void buffer_copycast(T1 *bufferto, const T2 *bufferfrom, const narray_size_t N)
|
||||||
|
{
|
||||||
|
narray_size_t I;
|
||||||
|
int J;
|
||||||
|
int nthreads;
|
||||||
|
std::vector<std::thread*> threads;
|
||||||
|
|
||||||
|
if(N<=0) return;
|
||||||
|
|
||||||
|
if(N<narray_thread_sz)
|
||||||
|
{
|
||||||
|
for(I=0;I<N;I++)
|
||||||
|
{
|
||||||
|
bufferto[I] = (T1) bufferfrom[I];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//threaded operation
|
||||||
|
|
||||||
|
nthreads = std::thread::hardware_concurrency();
|
||||||
|
nthreads = (nthreads<1) ? 1 : nthreads;
|
||||||
|
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
|
||||||
|
threads.resize(nthreads);
|
||||||
|
|
||||||
|
for(J=0;J<nthreads;J++)
|
||||||
|
{
|
||||||
|
threads[J] = new(std::nothrow) std::thread(
|
||||||
|
&buffer_copycast_tf<T1,T2>,
|
||||||
|
bufferto,
|
||||||
|
bufferfrom,
|
||||||
|
N,
|
||||||
|
J,nthreads
|
||||||
|
);
|
||||||
|
if(threads[J]==NULL)
|
||||||
|
{
|
||||||
|
printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(J=0;J<nthreads;J++)
|
||||||
|
{
|
||||||
|
if(threads[J]!=NULL)
|
||||||
|
{
|
||||||
|
threads[J]->join();
|
||||||
|
delete threads[J];
|
||||||
|
threads[J]= NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> void buffer_set_tf(
|
||||||
|
T *bufferto,
|
||||||
|
const T val,
|
||||||
|
const narray_size_t ind1,
|
||||||
|
const narray_size_t ind2,
|
||||||
|
const narray_size_t N,
|
||||||
|
int threadnum, int nthreads
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int I0,I1,Is,I;
|
||||||
|
|
||||||
|
Is = N/nthreads;
|
||||||
|
I0 = Is*threadnum;
|
||||||
|
I1 = (threadnum>=(nthreads-1))? N: Is*(threadnum+1);
|
||||||
|
|
||||||
|
I0 = I0 + ind1;
|
||||||
|
I1 = I1 + ind1;
|
||||||
|
|
||||||
|
for(I=I0;I<I1;I++)
|
||||||
|
{
|
||||||
|
bufferto[I] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> void buffer_set(T *buffer, const T val, const narray_size_t ind1, const narray_size_t ind2)
|
||||||
|
{
|
||||||
|
narray_size_t I;
|
||||||
|
int J;
|
||||||
|
int nthreads;
|
||||||
|
std::vector<std::thread*> threads;
|
||||||
|
|
||||||
|
narray_size_t N = ind2-ind1;
|
||||||
|
if(N<=0) return;
|
||||||
|
|
||||||
|
if(N<narray_thread_sz)
|
||||||
|
{
|
||||||
|
for(I=ind1;I<ind2;I++)
|
||||||
|
{
|
||||||
|
buffer[I] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//threaded operation
|
||||||
|
|
||||||
|
nthreads = std::thread::hardware_concurrency();
|
||||||
|
nthreads = (nthreads<1) ? 1 : nthreads;
|
||||||
|
nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads;
|
||||||
|
threads.resize(nthreads);
|
||||||
|
|
||||||
|
for(J=0;J<nthreads;J++)
|
||||||
|
{
|
||||||
|
threads[J] = new(std::nothrow) std::thread(
|
||||||
|
&buffer_set_tf<T>,
|
||||||
|
buffer,
|
||||||
|
val,
|
||||||
|
ind1,ind2,N,
|
||||||
|
J,nthreads
|
||||||
|
);
|
||||||
|
if(threads[J]==NULL)
|
||||||
|
{
|
||||||
|
printf("buffer_copy_cast: warning, thread %d failed to launch.\n",J);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(J=0;J<nthreads;J++)
|
||||||
|
{
|
||||||
|
if(threads[J]!=NULL)
|
||||||
|
{
|
||||||
|
threads[J]->join();
|
||||||
|
delete threads[J];
|
||||||
|
threads[J]= NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Binary file not shown.
@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
import os,sys,math
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
#Write double ndarray
|
||||||
|
def fwrite_dndarray(fp,array):
|
||||||
|
|
||||||
|
array = np.array(array,dtype=np.float64,order='F')
|
||||||
|
|
||||||
|
Ndim = np.int32(len(array.shape))
|
||||||
|
shp = np.int32(array.shape)
|
||||||
|
|
||||||
|
q = Ndim.tobytes()
|
||||||
|
fp.write(q)
|
||||||
|
if(Ndim>0):
|
||||||
|
q = shp.tobytes()
|
||||||
|
fp.write(q)
|
||||||
|
|
||||||
|
q = array.tobytes()
|
||||||
|
fp.write(q)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
#Read double ndarray
|
||||||
|
def fread_dndarray(fp):
|
||||||
|
|
||||||
|
array = np.zeros((),dtype=np.float64,order='F')
|
||||||
|
|
||||||
|
q = fp.read(4)
|
||||||
|
Ndim = np.frombuffer(q,dtype=np.int32,count=1)[0]
|
||||||
|
|
||||||
|
if(Ndim>0):
|
||||||
|
q = fp.read(4*Ndim)
|
||||||
|
shp = np.frombuffer(q,dtype=np.int32,count=Ndim)
|
||||||
|
piprod = 1
|
||||||
|
for i in range(0,len(shp)):
|
||||||
|
piprod = piprod*shp[i]
|
||||||
|
q = fp.read(8*piprod)
|
||||||
|
array = np.frombuffer(q,dtype=np.float64,count=piprod)
|
||||||
|
array = array.reshape(shp,order='F')
|
||||||
|
|
||||||
|
return array
|
||||||
|
|
||||||
|
#Write float ndarray
|
||||||
|
def fwrite_fndarray(fp,array):
|
||||||
|
|
||||||
|
array = np.array(array,dtype=np.float32,order='F')
|
||||||
|
|
||||||
|
Ndim = np.int32(len(array.shape))
|
||||||
|
shp = np.int32(array.shape)
|
||||||
|
|
||||||
|
q = Ndim.tobytes()
|
||||||
|
fp.write(q)
|
||||||
|
if(Ndim>0):
|
||||||
|
q = shp.tobytes()
|
||||||
|
fp.write(q)
|
||||||
|
|
||||||
|
q = array.tobytes()
|
||||||
|
fp.write(q)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
#Read float ndarray
|
||||||
|
def fread_fndarray(fp):
|
||||||
|
|
||||||
|
array = np.zeros((),dtype=np.float32,order='F')
|
||||||
|
|
||||||
|
q = fp.read(4)
|
||||||
|
Ndim = np.frombuffer(q,dtype=np.int32,count=1)[0]
|
||||||
|
|
||||||
|
if(Ndim>0):
|
||||||
|
q = fp.read(4*Ndim)
|
||||||
|
shp = np.frombuffer(q,dtype=np.int32,count=Ndim)
|
||||||
|
piprod = 1
|
||||||
|
for i in range(0,len(shp)):
|
||||||
|
piprod = piprod*shp[i]
|
||||||
|
q = fp.read(4*piprod)
|
||||||
|
array = np.frombuffer(q,dtype=np.float32,count=piprod)
|
||||||
|
array = array.reshape(shp,order='F')
|
||||||
|
|
||||||
|
return array
|
@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/bash
|
||||||
|
|
||||||
|
import os,sys,math
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from amsbinarrayrw import *
|
||||||
|
|
||||||
|
def plot_random1():
|
||||||
|
|
||||||
|
fp = open("./build_linux64/tmp_testrand.bin","r+b")
|
||||||
|
data = fread_dndarray(fp)
|
||||||
|
N1 = data.shape[0]
|
||||||
|
N2 = int(math.sqrt(N1))
|
||||||
|
N3 = N2*N2
|
||||||
|
data = data[0:N3]
|
||||||
|
data = data.reshape([N2,N2])
|
||||||
|
q = np.zeros([N2,N2,3])
|
||||||
|
q[:,:,0] = data[:,:]
|
||||||
|
q[:,:,1] = data[:,:]
|
||||||
|
q[:,:,2] = data[:,:]
|
||||||
|
|
||||||
|
hf = plt.figure(1)
|
||||||
|
plt.imshow(q)
|
||||||
|
plt.show()
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
if(__name__=="__main__"):
|
||||||
|
plot_random1()
|
||||||
|
|
||||||
|
pass
|
Loading…
Reference in New Issue