Ejemplo n.º 1
0
Reply::Reply(boost::asio::mutable_buffer mbuffer)
{
  from_buffer(mbuffer);
}
Ejemplo n.º 2
0
// global reducing function
template <typename T> T global(T& value, const char* operation) {
	// initialize global value
	T global = value;

#ifdef MPI_VERSION
	int id = MPI::COMM_WORLD.Get_rank();
	int np = MPI::COMM_WORLD.Get_size();

	if (id == 0) {
		// receive local values
		for (int i = 1; i < np; i++) {
			T temp;
			int size;
			MPI::COMM_WORLD.Recv(&size, 1, MPI_INT, i, 100);
			char* buffer = new char[size];
			MPI::COMM_WORLD.Recv(buffer, size, MPI_CHAR, i, 200);
			from_buffer(temp, buffer);
			delete [] buffer;

			// perform operation
			if (std::string(operation)=="add" or std::string(operation)=="sum")
				global += temp;
			else if (std::string(operation)=="min" or std::string(operation)=="minimum")
				global = min(global, temp);
			else if (std::string(operation)=="max" or std::string(operation)=="maximum")
				global = max(global, temp);
		}

		// send global value
		for (int i = 1; i < np; i++) {
			int size = buffer_size(global);
			MPI::COMM_WORLD.Send(&size, 1, MPI_INT, i, 300);
			char* buffer = new char[size];
			to_buffer(global, buffer);
			MPI::COMM_WORLD.Send(buffer, size, MPI_CHAR, i, 400);
			delete [] buffer;
		}
	}

	else {
		// send local value
		int size = buffer_size(value);
		MPI::COMM_WORLD.Send(&size, 1, MPI_INT, 0, 100);
		char* buffer = new char[size];
		to_buffer(value, buffer);
		MPI::COMM_WORLD.Send(buffer, size, MPI_CHAR, 0, 200);
		delete [] buffer;

		// receive global value
		MPI::COMM_WORLD.Recv(&size, 1, MPI_INT, 0, 300);
		buffer = new char[size];
		MPI::COMM_WORLD.Recv(buffer, size, MPI_CHAR, 0, 400);
		from_buffer(global, buffer);
		delete [] buffer;
	}

	MPI::COMM_WORLD.Barrier();
#endif

	return global;
}