Skip to content

Commit

Permalink
update path add put get
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhezhe committed Feb 24, 2019
1 parent f55f27c commit 105e865
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/Python/dataspaces/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
AUTOMAKE_OPTIONS = foreign

PYTHON_CONFIG = $(PYTHON) ../python-config
MPI4PY_INCLUDE = ${shell $(PYTHON) -c 'import mpi4py; print( mpi4py.get_include() )'}
NUMPY_INCLUDE = ${shell $(PYTHON) -c 'import numpy; print( numpy.get_include() )'}
PREINCLUDES = -I${abs_top_srcdir}/include
LFLAG = -L${abs_top_srcdir}/src -ldspaces -ldscommon -ldspacesf -L${abs_top_srcdir}/dart -ldart $(DSPACESLIB_LDADD)

SWIG = swig
SWIG_PY = ${SWIG} -python
.PHONY: src
src: dspaces_wrap.c
dspaces_wrap.c: dspaces.i
[ -f ./numpy.i ] && echo "numpy.i already here, good" || curl -O https://raw.githubusercontent.com/numpy/numpy/master/tools/swig/numpy.i
${SWIG_PY} -I${MPI4PY_INCLUDE} -I${NUMPY_INCLUDE} $(PREINCLUDES) -o $@ $<

MPICC = mpicc
CFLAGS = -fPIC ${shell ${PYTHON_CONFIG} --includes} -Wall -g
LDFLAGS = -shared ${shell ${PYTHON_CONFIG} --libs}
SO = ${shell ${PYTHON_CONFIG} --extension-suffix}

.PHONY: buildwrap
buildwrap: _dspaces${SO}
_dspaces${SO}: dspaces_wrap.c
${MPICC} ${CFLAGS} -I${MPI4PY_INCLUDE} -I${NUMPY_INCLUDE} $(PREINCLUDES) dspaces_wrap.c $(LFLAG) ${LDFLAGS} -o _dspaces${SO}

all: buildwrap

install-exec-local:
cd .. && python3 setup.py install

clean-local:
${RM} -r dspaces_wrap.c dspaces.py* _dspaces*.so *pycache*
1 change: 1 addition & 0 deletions tests/Python/dataspaces/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "dataspaces"
110 changes: 110 additions & 0 deletions tests/Python/dataspaces/dataspaceClient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

import numpy as np
import dataspaces.dspaces as dspaces
import ctypes
import copy

class dataspaceClient:
def __init__(self, appid, comm):
self.appid = appid
self.comm = comm
self.num_peers = comm.Get_size()
print("init num_peers")
print(self.num_peers)
self.init(comm,self.num_peers,appid)

def getElemNum(self,lb,ub):
# get elem number between lb and ub
dim = len(lb)
elemNum = 1
for i in range (dim):
dis = ub[i]-lb[i] + 1
elemNum = elemNum*dis

return elemNum

def getDataShape(self,lb,ub):
shape = []
dim = len(lb)
for i in range (dim):
shape.append(ub[i]-lb[i] + 1)

return tuple(shape)

# input a multidimensional array and get the size of it
# input data should be an nparray
def getUpBound(self,lb,data):

shape = data.shape
if(len(lb)!=len(shape)):
print ('data shape should in the same dimention with lb')
exit(-1)

dim = len(shape)
offset = np.ones(dim)

ub = np.asarray(lb)+np.asarray(shape) - offset
ub = ub.astype(int)
return ub.tolist(),dim

def init(self,comm,num_peers,appid):
dspaces.wrapper_dspaces_init(comm,num_peers,appid)

def finalize(self):
dspaces.wrapper_finalize()

def lock_on_write(self,lock_name):
dspaces.wrapper_dspaces_lock_on_write(self.comm, lock_name)

def unlock_on_write(self,lock_name):
dspaces.wrapper_dspaces_unlock_on_write(self.comm, lock_name)

def get(self,var_name,ver,lb,ub):

# originalShape = data.shape
# ub,ndim = this.getUpBound(lb,data)
if(len(lb)!=len(ub)):
sys.exit("lb and ub should in same dimention")

ndim = len(lb)

originalShape = self.getDataShape(lb,ub)
elemNum = self.getElemNum(lb,ub)

arraydataFlaten = np.empty(elemNum)
arraydataFlaten.fill(0.0)


elemsize = ctypes.sizeof(ctypes.c_double)

# the elemsize here is the size for each element
dspaces.wrapper_get_data(var_name,ver,elemsize,ndim,lb,ub,arraydataFlaten)

# reshape into original format
getdata = arraydataFlaten.reshape(originalShape)
return getdata


def put(self,var_name,ver,lb,putdata):

arraydata = np.asarray(putdata)
ub,ndim = self.getUpBound(lb,arraydata)

# transfer to one dimention
arraydata = arraydata.flatten()

elemsize = ctypes.sizeof(ctypes.c_double)

# the elemsize here is the size for each element
dspaces.wrapper_put_data(var_name,ver,elemsize,ndim,lb,ub,arraydata)

def lock_on_read(self,lock_name):
dspaces.wrapper_dspaces_lock_on_read(self.comm, lock_name)

def unlock_on_read(self,lock_name):
dspaces.wrapper_dspaces_unlock_on_read(self.comm, lock_name)





112 changes: 112 additions & 0 deletions tests/Python/dataspaces/dspaces.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* put.c : Example 1: DataSpaces put tutorial
* This example will show you the simplest way
* to put a 1D array of 3 elements into the DataSpace.
* */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "dataspaces.h"
#include "mpi.h"

void wrapper_dspaces_init(MPI_Comm pgcomm, int num_peers, int appid)
{
int nprocs;
int rank;
MPI_Comm_size(pgcomm, &nprocs);
MPI_Comm_rank(pgcomm, &rank);
MPI_Comm gcomm = pgcomm;
MPI_Barrier(gcomm);

printf("num_peers %d appid %d\n", num_peers, appid);

// Initalize DataSpaces
// # of Peers, Application ID, ptr MPI comm, additional parameters
// # Peers: Number of connecting clients to the DS server
// Application ID: Unique idenitifier (integer) for application
// Pointer to the MPI Communicator, allows DS Layer to use MPI barrier func
// Addt'l parameters: Placeholder for future arguments, currently NULL.
// the first parameter should be same with the number of threads to access server (namely -n after mpiexec)
// the second parameter is used to label current application, one application/program should have unique appid
// init is only needed to call once for multiple timesteps
dspaces_init(num_peers, appid, &gcomm, NULL);

return;
}

int wrapper_put_data(const char *var_name,
unsigned int timestep, int size,
int ndim, unsigned long long *lb, int n1, unsigned long long *ub, int n2, double *data, int n)
{

//printf("Timestep %d: put data %lf %lf %lf\n", timestep, data[0], data[1], data[2]);
//TODO if debug
/*
int i=0;
for (i=0;i<ndim;i++){
printf("index lb %d is %d\n", i, lb[i]);
}
for (i=0;i<ndim;i++){
printf("index ub %d is %d\n", i, ub[i]);
}
*/


return dspaces_put(var_name, timestep, size, ndim, (uint64_t *)lb, (uint64_t *)ub, data);
}

int wrapper_get_data(const char *var_name,
unsigned int timestep, int size,
int ndim, unsigned long long *lb, int n1, unsigned long long *ub, int n2, double *data, int n)
{

//TODO if debug
/*
int i=0;
for (i=0;i<ndim;i++){
printf("index lb %d is %d\n", i, lb[i]);
}
for (i=0;i<ndim;i++){
printf("index ub %d is %d\n", i, ub[i]);
}
*/

int rcode = dspaces_get(var_name, timestep, size, ndim, (uint64_t *)lb, (uint64_t *)ub, data);
return rcode;
}

void wrapper_dspaces_lock_on_write(MPI_Comm pgcomm, char *varname)
{
//printf("get varname %s\n", varname);
//MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_lock_on_write(varname, &pgcomm);
return;
}

void wrapper_dspaces_unlock_on_write(MPI_Comm pgcomm, char *varname)
{
//MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_unlock_on_write(varname, &pgcomm);
return;
}

void wrapper_dspaces_lock_on_read(MPI_Comm pgcomm, char *varname)
{
//printf("get varname %s\n", varname);
//MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_lock_on_read(varname, &pgcomm);
return;
}

void wrapper_dspaces_unlock_on_read(MPI_Comm pgcomm, char *varname)
{
dspaces_unlock_on_read(varname, &pgcomm);
return;
}

void wrapper_finalize()
{
dspaces_finalize();
return;
}
45 changes: 45 additions & 0 deletions tests/Python/dataspaces/dspaces.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%module dspaces

%{
#define SWIG_FILE_WITH_INIT
#define SWIG_PYTHON_STRICT_BYTE_CHAR
#include "mpi.h"
#include "dspaces.c"
%}


%include mpi4py/mpi4py.i
%include "numpy.i"

%init %{
import_array();
%}


%mpi4py_typemap(Comm, MPI_Comm);

%apply (double* IN_ARRAY1, int DIM1) {(double* data, int n)};

%apply (unsigned long long * IN_ARRAY1, int DIM1) {(unsigned long long * lb, int n1)};

%apply (unsigned long long * IN_ARRAY1, int DIM1) {(unsigned long long * ub, int n2)};

void wrapper_dspaces_init(MPI_Comm pgcomm,int num_peers, int appid);

void wrapper_dspaces_lock_on_write(MPI_Comm pgcomm, char *varname);

void wrapper_dspaces_unlock_on_write(MPI_Comm pgcomm, char *varname);

void wrapper_dspaces_lock_on_read(MPI_Comm pgcomm, char *varname);

void wrapper_dspaces_unlock_on_read(MPI_Comm pgcomm, char *varname);

void wrapper_finalize();

int wrapper_put_data(const char *var_name,
unsigned int ver, int size,
int ndim, unsigned long long *lb,int n1, unsigned long long*ub, int n2, double *data, int n);

int wrapper_get_data(const char *var_name,
unsigned int ver, int size,
int ndim, unsigned long long *lb,int n1, unsigned long long*ub, int n2, double *data, int n);
19 changes: 19 additions & 0 deletions tests/Python/dataspaces/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from mpi4py import MPI
import numpy as np
import dataspaces.dataspaceClient as dataspaces

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
appid = 2

ds = dataspaces.dataspaceClient(appid, comm)

var_name = "ex1_sample_data"

for ts in range(10):
ds.lock_on_read("my_test_lock")
lb = [rank+ts*3]
ub = [rank+ts*3+2]
data = ds.get(var_name, ts, lb, ub)
print("Timestep %d: get data %s" % (ts, data))
ds.unlock_on_read("my_test_lock")
File renamed without changes.
20 changes: 20 additions & 0 deletions tests/Python/dataspaces/put.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mpi4py import MPI
import numpy as np
import dataspaces.dataspaceClient as dataspaces

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
appid = 1

var_name = "ex1_sample_data"

ds = dataspaces.dataspaceClient(appid, comm)
for ts in range(10):
ds.lock_on_write("my_test_lock")
data = np.random.randint(0, 99, size=3)
print("Timestep %d: put data %s" % (ts, data))
lb = [rank+ts*3]
print(len(np.asarray(lb).shape))
print(len(data.shape))
ds.put(var_name, ts, lb, data)
ds.unlock_on_write("my_test_lock")
Loading

0 comments on commit 105e865

Please sign in to comment.