-
Notifications
You must be signed in to change notification settings - Fork 24
/
asynch_interface_py.c
97 lines (72 loc) · 2.78 KB
/
asynch_interface_py.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "asynch_interface_py.h"
//Utilities for mixing C and Python **************************************
void C_inc_ref(PyObject* obj)
{
Py_INCREF(obj);
}
unsigned int* Allocate_CUINT_Array(unsigned int n)
{
return (unsigned int*) malloc(n*sizeof(unsigned int));
}
void Free_PythonInterface(asynchsolver* asynch)
{
free((PythonInterface*)(asynch->ExternalInterface));
}
//Routines for the Python interface **************************************
asynchsolver* Asynch_Init_py(int numprocs,int* ranks)
{
//!!!! Assumes MPI_COMM_WORLD. This should create a new communicator and pass that along. !!!!
asynchsolver* asynch = Asynch_Init(MPI_COMM_WORLD,NULL,NULL);
asynch->ExternalInterface = malloc(sizeof(PythonInterface));
return asynch;
}
void Asynch_Set_System_State_py(asynchsolver* asynch,double t_0,double* values)
{
unsigned int i,j,N = asynch->N,*assignments = asynch->assignments;
VEC** array = (VEC**) malloc(N*sizeof(VEC*));
Link* current;
//Convert input to vectors
for(i=0;i<N;i++)
{
if(assignments[i] == my_rank)
{
current = asynch->sys[i];
array[i] = v_get(current->dim);
for(j=0;j<current->dim;j++) array[i]->ve[j] = values[i*current->dim+j];
}
else
array[i] = NULL;
}
Asynch_Set_System_State(asynch,t_0,array);
//Trash vectors
for(i=0;i<N;i++) free(array[i]);
free(array);
}
//Custom Model routines *******************************************
int Asynch_Custom_Model_py(asynchsolver* asynch,void (*SetParamSizes)(UnivVars*,PyObject*),void (*Convert)(VEC*,unsigned int,void*),void (*Routines)(Link*,unsigned int,unsigned int,unsigned short int,void*),
void (*Precalculations)(Link*,VEC*,VEC*,unsigned int,unsigned int,unsigned short int,unsigned int,void*),int (*InitializeEqs)(VEC*,VEC*,QVSData*,unsigned short int,VEC*,unsigned int,unsigned int,unsigned int,void*,void*),PyObject* lib)
{
//Setup the python interface data
PythonInterface* external = (PythonInterface*) asynch->ExternalInterface;
external->lib = lib;
external->SetParamSizes_func = SetParamSizes;
external->Routines_func = Routines;
//Set the functions
int val = Asynch_Custom_Model(asynch,&SetParamSizes_py,Convert,&InitRoutines_py,Precalculations,InitializeEqs);
return val;
}
//Called by solvers
void SetParamSizes_py(UnivVars* GlobalVars,void* user)
{
PythonInterface* external = (PythonInterface*) user;
external->SetParamSizes_func(GlobalVars,external->lib);
}
void InitRoutines_py(Link* link,unsigned int type,unsigned int exp_imp,unsigned short int dam,void* user)
{
PythonInterface* external = (PythonInterface*) user;
external->Routines_func(link,type,exp_imp,dam,external->lib);
}
void Asynch_Copy_Local_OutputUser_Data_py(asynchsolver* asynch,unsigned int location,PyObject* source,unsigned int size)
{
Asynch_Copy_Local_OutputUser_Data(asynch,location,(void*) source,size);
}