Skip to content

Commit

Permalink
heat example. dco interface adapted for develop branch. dco master in…
Browse files Browse the repository at this point in the history
…terface added
  • Loading branch information
Michel Schanen committed Apr 17, 2014
1 parent 8df0093 commit 8e6dfe8
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 11 deletions.
7 changes: 3 additions & 4 deletions examples/Makefile.inc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
CC=mpicc
CXX=mpicxx
CFLAGS=-g -O0 -fopenmp
CXXFLAGS=-g -O0 -fopenmp
CFLAGS=-g -O0 -DDCO_DEFAULT
CXXFLAGS=-g -O0 -DDCO_DEFAULT

AMPI_LIBDIR=../../src
AMPI_INCLUDE=../../include

DCO_LIBDIR=$(HOME)/git/dco/build/dco_cpp/lib
DCO_INCLUDE=$(HOME)/git/dco/build/dco_cpp/include
DCO_INCLUDE=$(DCO_BASE_DIR)/src/dco
4 changes: 2 additions & 2 deletions examples/heat_dco/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ all: sd


sd: sd.o f.o ampi_interface.o
$(CXX) $(CFLAGS) -I$(AMPI_INCLUDE) -I$(DCO_INCLUDE) -L$(AMPI_LIBDIR) -o $@ $^ $(DCO_LIBDIR)/libdco.a -lm -lAMPI
$(CXX) $(CFLAGS) -I$(AMPI_INCLUDE) -I$(DCO_INCLUDE) -L$(AMPI_LIBDIR) -o $@ $^ -lm -lAMPI
sd.o: sd.cpp
$(CXX) $(CFLAGS) -I$(AMPI_INCLUDE) -I$(DCO_INCLUDE) -c $^
f.o: f.cpp
$(CXX) $(CFLAGS) -I$(AMPI_INCLUDE) -I$(DCO_INCLUDE) -c $^
ampi_interface.o: ampi_interface.cc
ampi_interface.o: ampi_interface.cpp
$(CXX) $(CFLAGS) -I$(AMPI_INCLUDE) -I$(DCO_INCLUDE) -c $^

clean:
Expand Down
1 change: 1 addition & 0 deletions examples/heat_dco/ampi_interface.cpp
5 changes: 3 additions & 2 deletions examples/heat_dco/sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ using namespace std;

//#include "dco_tape.hpp"
#include "f.hpp"
#define TAPE_MODE 1
#define DERIV_MODE 1


dco::a1s::tape *dco::a1s::global_tape=0;

int myid, numprocs;
/*
Expand Down
5 changes: 2 additions & 3 deletions interfaces/dco/ampi_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ extern "C" {
}
};

void ampi_tape_wrapper(tape &caller, const tape::interpretation_settings &settings, dco::a1s::tape::external_function_base_data *userdata) {
AMPI_data *data = static_cast<AMPI_data*>(userdata);
void ampi_tape_wrapper(AMPI_data *data) {
ampi_interpret_tape(data->idx);
}

Expand All @@ -72,7 +71,7 @@ extern "C" {
return;
}
//todo: insert an external function handler!!!
global_tape->register_external_function(&ampi_tape_wrapper, new AMPI_data(*i));
AMPI_data *D = global_tape->create_ext_fcn_data<AMPI_data>(ampi_tape_wrapper, *i);
// ampi_counter++;
// std::cout << "ampi_counter: " << ampi_counter << endl;

Expand Down
129 changes: 129 additions & 0 deletions interfaces/dco/ampi_interface_master.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include "dco.hpp"
#include "ampi_tape.hpp"

//#define INT64 int

using namespace dco::a1s;

extern "C" {
//forward declare von AMPI

#ifndef DCO_AMPI
//void ampi_interpret_tape(long int idx) {}
#endif
//long int ampi_counter=0;


void ampi_get_val(void *buf, int *i, double *x) {
*x=static_cast<type*>(buf)[*i]._value();
}
void ampi_set_val(void* buf, int *i, double *v) {
type &dummy= static_cast<type*>(buf)[*i];
*const_cast<double*>(&(dummy._value())) = *v;
}

void ampi_get_idx(void *buf, int *i, INT64 *idx) {
type &var = static_cast<type*>(buf)[*i];
if(!var._data()._is_registered()) {
*idx=0;
}
else {
// *idx=&(global_tape->_adjoint(var._data().tape_index()));
*idx = var._data().tape_index();
}

}


void ampi_get_adj(INT64 *idx, double *x) {
//if(*idx) *x = *(*idx);
if(*idx!=0) *x = dco::a1s::global_tape->_adjoint(*idx);
//std::cout << "get adj: " << *(*idx) << " idx=" << *idx << std::endl;
//std::cout << "AMPI_GET_ADJ: " << *x << std::endl;
}
void ampi_set_adj(INT64 *idx, double *x) {
//if(*idx!=0) const_cast<double&>(dco::a1s::global_tape->_adjoint(*idx)) = *x;
if(*idx!=0) dco::a1s::global_tape->_adjoint(*idx) += *x;
// if(*idx) *(*idx)=*x;
//std::cout << "set adj: " << *x << " idx=" << *idx << std::endl;
}


extern "C" void ampi_reset_entry(long int idx);

struct AMPI_data : tape::external_function_base_data {
int idx;
AMPI_data(const int nidx) : idx(nidx) {}
virtual ~AMPI_data() {
//std::cout << "ampi_reset_entry with idx=" << idx << std::endl;
ampi_reset_entry(idx);
}
};

void ampi_tape_wrapper(tape &caller, const tape::interpretation_settings &settings, dco::a1s::tape::external_function_base_data *userdata) {
AMPI_data *data = static_cast<AMPI_data*>(userdata);
ampi_interpret_tape(data->idx);
}


void ampi_create_tape_entry(long int *i) {
if(!global_tape->is_active()) {
// std::cout << "tape is passive, not AMPI Callback will be created!" << std::endl;
return;
}
//todo: insert an external function handler!!!
global_tape->register_external_function(&ampi_tape_wrapper, new AMPI_data(*i));
// ampi_counter++;
// std::cout << "ampi_counter: " << ampi_counter << endl;

//this will call ampi_interpret_tape
//std::cout << "i: " << *i << endl;
}

void ampi_create_dummies(void *buf, int *size) {
type *values=static_cast<type*>(buf);

for(int i=0;i<*size;++i) {
type &dummy=values[i];
//std::cout << "dummy.value=" << dummy._value << " .tapeindex=" << dummy._data.tape_index << std::endl;
//std::cout << "mpi_global_tape=" << mpi_global_tape << std::endl;
//if(dummy._edgecount==0) {
dummy=0;
global_tape->register_variable(dummy);
}
}

int ampi_is_tape_active () {
if (NULL != global_tape) {
#ifdef DCO_ALLOW_TAPE_SWITCH_OFF
return global_tape->is_active();
#else
return 1;
#endif
} else {
return 0;
}
}

// const int n=*size;
// type *actives=static_cast<type*>(buf);

// int startindex=0;
// dco::a1s::tape::TAPE_ENTRY *ins=global_tape->_get_insert_ptr_range(n, startindex );

// #ifdef DCO_OPEN_MP
// #pragma omp parallel for
// #endif
// for(int i=0;i<n;++i) {
// ins[i].arg=0; //edgecount =0
// actives[i] = 0;

// dco::a1s::data &data = const_cast<dco::a1s::data&>(actives[i]._data());
// data.register_variable( startindex+i ,global_tape);
// }


//}


}

0 comments on commit 8e6dfe8

Please sign in to comment.