diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4a46d0c21..2419d8a0c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,7 +7,7 @@ LIST(APPEND example_files test_api_multiple.c test_interpose.c test_interpose_multiple.c - test_ckpt.C + test_ckpt.cpp test_ckpt.F scr.moab scr_interpose.moab @@ -70,18 +70,18 @@ ADD_EXECUTABLE(test_interpose_multiple test_common.c test_interpose_multiple.c) TARGET_LINK_LIBRARIES(test_interpose_multiple ${SCR_LINK_TO}) SCR_ADD_TEST(test_interpose_multiple "" "../examplesrank*.ckpt") -ADD_EXECUTABLE(test_ckpt_C test_ckpt.C) +ADD_EXECUTABLE(test_ckpt test_ckpt.cpp) IF(MPI_CXX_FOUND) - TARGET_INCLUDE_DIRECTORIES(test_ckpt_C PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${MPI_CXX_INCLUDE_PATH}) - TARGET_LINK_LIBRARIES(test_ckpt_C ${SCR_LINK_TO} ${MPI_CXX_LIBRARIES}) + TARGET_INCLUDE_DIRECTORIES(test_ckpt PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${MPI_CXX_INCLUDE_PATH}) + TARGET_LINK_LIBRARIES(test_ckpt ${SCR_LINK_TO} ${MPI_CXX_LIBRARIES}) ELSE(MPI_CXX_FOUND) - TARGET_LINK_LIBRARIES(test_ckpt_C ${SCR_LINK_TO}) + TARGET_LINK_LIBRARIES(test_ckpt ${SCR_LINK_TO}) ENDIF(MPI_CXX_FOUND) -SCR_ADD_TEST(test_ckpt_C "" "rank_0.ckpt") +SCR_ADD_TEST(test_ckpt "" "rank_0.ckpt") IF(ENABLE_FORTRAN) SET_SOURCE_FILES_PROPERTIES(test_ckpt.F PROPERTIES LANGUAGE Fortran) - ADD_EXECUTABLE(test_ckpt_F test_ckpt.F) + ADD_EXECUTABLE(test_ckpt_F test_ckpt.F) SET_TARGET_PROPERTIES(test_ckpt_F PROPERTIES LINKER_LANGUAGE Fortran) IF(SCR_LINK_STATIC) diff --git a/examples/test_ckpt.C b/examples/test_ckpt.C deleted file mode 100644 index 3c234ab91..000000000 --- a/examples/test_ckpt.C +++ /dev/null @@ -1,70 +0,0 @@ -// mpig++ -g -O0 -I../src -o test_ckpt_C test_ckpt.C -L/usr/local/tools/scr-1.1/lib -lscr - -#include -#include -#include -#include - -#include "mpi.h" -#include "scr.h" - -using namespace std; - -int checkpoint() -{ - // inform SCR that we are starting a new checkpoint - SCR_Start_checkpoint(); - - // get our rank - int rank; - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - - // build the filename for our checkpoint file - stringstream tmp; - tmp << "rank_" << rank << ".ckpt"; - string name = tmp.str(); -cout << "File: " << name << endl; - - // register our checkpoint file with SCR, - // and ask SCR where to write the file - char file[SCR_MAX_FILENAME]; - SCR_Route_file(name.c_str(), file); -cout << "File: " << file << endl; - - // write our checkpoint file - int valid = 1; - - ofstream ckpt; - ckpt.open(file, ios::out | ios::trunc); - ckpt << "hi" << endl; - ckpt.close(); - - // inform SCR whether this process wrote each of its - // checkpoint files successfully - SCR_Complete_checkpoint(valid); - - return 0; -} - -int main() -{ - MPI_Init(NULL, NULL); - - // initialize the SCR library - SCR_Init(); - - // ask SCR whether we need to checkpoint - int flag = 0; - SCR_Need_checkpoint(&flag); - if (flag) { - // execute the checkpoint code - checkpoint(); - } - - // shut down the SCR library - SCR_Finalize(); - - MPI_Finalize(); - - return 0; -} diff --git a/examples/test_ckpt.cpp b/examples/test_ckpt.cpp new file mode 100644 index 000000000..7e1812e7a --- /dev/null +++ b/examples/test_ckpt.cpp @@ -0,0 +1,90 @@ +// mpig++ -g -O0 -I../src -o test_ckpt_C test_ckpt.C -L/usr/local/tools/scr-1.1/lib -lscr +/* + * Usage: + * + * ./test_ckpt [megabytes] + * + * Optionally pass the size of our checkpoint file to write, in megabytes. + * + * Note: This is compiled as C++ file to verify SCR works with C++. It only + * trivially uses C++ semantics, so that people can use it as example code for + * C as well. + */ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#include "mpi.h" +#include "scr.h" +int checkpoint(int size_mb) +{ + int rank; + char tmp[256]; + char file[SCR_MAX_FILENAME]; + char dir[SCR_MAX_FILENAME]; + char dname; + int rc; + + /* Get our rank */ + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + /* Inform SCR that we are starting a new checkpoint */ + SCR_Start_checkpoint(); + + /* Build the filename for our checkpoint file */ + sprintf(tmp, "rank_%d", rank); + cout << "In: " << tmp << "\n"; + + /* Register our checkpoint file with SCR, and where to write it */ + SCR_Route_file(tmp, file); + + /* Write our checkpoint file */ + sprintf(tmp, "truncate -s %dM %s", size_mb, file); + system(tmp); + + cout << "Out: " << file << "\n"; + + /* Tell SCR whether this process wrote its checkpoint files successfully */ + SCR_Complete_checkpoint(1); + + return 0; +} + +/* + * test_ckpt [megabytes] + * + * Optionally pass the size of our checkpoint file to write, in megabytes. + */ +int main(int argc, char **argv) +{ + int size_mb = 1; + + if (argc == 2) { + size_mb = atoi(argv[1]); + } + + MPI_Init(NULL, NULL); + + /* Initialize the SCR library */ + SCR_Init(); + + /* Ask SCR whether we need to checkpoint */ + int flag = 0; + SCR_Need_checkpoint(&flag); + if (flag) { + /* Execute the checkpoint code */ + checkpoint(size_mb); + } + + /* Shut down the SCR library */ + SCR_Finalize(); + + MPI_Finalize(); + + return 0; +}