/*
export DG_DIR=/nobackup/kdgordon/dirtygrid
export DMTCP_DIR=$DG_DIR/software/dmtcp-1.2.8-debug
g++ -Wall -Wextra -O2 -L$DMTCP_DIR/lib -I$DMTCP_DIR/include -lmtcp -o test_ckpt test_ckpt.cpp
$DG_DIR/bin/timeout -s KILL 110 env CKPT_TIME=60 LD_LIBRARY_PATH=$DMTCP_DIR/lib ./test_ckpt
$DG_DIR/bin/timeout -s KILL 110 $DMTCP_DIR/bin/mtcp_restart checkpoint.mtcp
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "mtcp.h"

using std::cout;
using std::endl;

int main() 
{
	// MTCP checkpointing setup
	const char* ckpt_time_str;
	ckpt_time_str = getenv("CKPT_TIME");
	if (ckpt_time_str == NULL) ckpt_time_str = "3600";
	int ckpt_time = atoi(ckpt_time_str);
	if (ckpt_time == 0) ckpt_time = 3600;
	cout << "ckpt_time: " << ckpt_time << endl;
	mtcp_init ("checkpoint.mtcp", ckpt_time, 0);
	mtcp_ok();

	// dummy calculation
	int n = 104857600;
	cout << "array_size: " << n*sizeof(double) << endl;
	double *x = new double[n];
	for (int i = 0; i < n; i++) {
		x[i] = i;
	}
	while (1) {
		cout << '.';
		cout.flush();
		for (int i = 0; i < n; i++) {
			x[i] = cos(x[i]);
			if (x[i] < 0) break; // impossible condition, just to prevent dead code elimination
		}
	}
	
	return 0;
}
