void runPeer(igcl::Peer * peer)
{
	peer->start();

	Raytracer raytracer;
	raytracer.setupScene();	// setup scene objects

	timeval globalIni, end;
	gettimeofday(&globalIni, NULL);

	imageSize = IMAGE_HEIGHT*IMAGE_WIDTH;

	while (1)
	{
		uint startIndex = 0;
		igcl::result_type res = peer->waitRecvFrom(0, startIndex);
		if (res != igcl::SUCCESS)
			break;
		//std::cout << "received " << startIndex << endl;

		uint nIndexes = std::min(blockSize, imageSize-startIndex);
		uint endIndex = startIndex + nIndexes;

		color_s_char array[nIndexes];

		for (uint i=startIndex; i<endIndex; ++i) {
			int row = i / IMAGE_WIDTH;
			int col = i % IMAGE_WIDTH;
			const color_s & c = raytracer.castRayFromScreen(row, col);		// calculate color of pixel (CPU-HEAVY)
			array[i-startIndex] = color_s_char(c);
		}

		peer->sendTo(0, array+0, nIndexes);
	}

	gettimeofday(&end, NULL);
	cout << "total time:\t" << timeDiff(globalIni, end) << " ms\n";

	peer->hang();
}
void runCoordinator(igcl::Coordinator * node)
{
	const uint blockSize = 10000;
	Raytracer raytracer;
	raytracer.setupScene();	// setup scene objects

	for (int t=0; t<30; t++) {
		timeval iniTime;
		start(iniTime);

		//#pragma omp parallel for num_threads(N_THREADS)
		#pragma omp parallel for schedule(dynamic, 10000) num_threads(N_THREADS)
		for (uint i=0; i<imageSize; i++)
		{
			int row = i / IMAGE_WIDTH;
			int col = i % IMAGE_WIDTH;
			color_s c = raytracer.castRayFromScreen(row, col);	// calculate color of pixel (CPU-HEAVY)
			image->setPixel(row, col, c.r, c.g, c.b);
		}

		finish(iniTime);
	}
}