- return smpi_mpi_comm_rank(comm, SIMIX_host_self());
-}
-
-//int smpi_mpi_comm_world_rank_self()
-//{
-// return smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, SIMIX_host_self());
-//}
-
-int smpi_sender(int argc, char **argv)
-{
- smx_process_t self;
- smx_host_t shost;
- int rank;
-
- xbt_fifo_t request_queue;
- smx_mutex_t request_queue_mutex;
- int size;
-
- int running_hosts_count;
-
- smpi_mpi_request_t *request;
-
- smx_host_t dhost;
-
- smx_action_t communicate_action;
-
- smpi_received_message_t *message;
-
- int drank;
-
- smx_process_t receiver_process;
-
- self = SIMIX_process_self();
- shost = SIMIX_host_self();
- rank = smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, shost);
-
- // make sure root is done before own initialization
- SIMIX_mutex_lock(smpi_global->start_stop_mutex);
- if (!smpi_global->root_ready) {
- SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
- }
- SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
-
- request_queue = smpi_global->pending_send_request_queues[rank];
- request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[rank];
- size = smpi_mpi_comm_size(smpi_mpi_global->mpi_comm_world);
-
- smpi_global->sender_processes[rank] = self;
-
- // wait for all nodes to signal initializatin complete
- SIMIX_mutex_lock(smpi_global->start_stop_mutex);
- smpi_global->ready_process_count++;
- if (smpi_global->ready_process_count < 3 * size) {
- SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
- } else {
- SIMIX_cond_broadcast(smpi_global->start_stop_cond);
- }
- SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
-
- do {
-
- SIMIX_mutex_lock(request_queue_mutex);
- request = xbt_fifo_shift(request_queue);
- SIMIX_mutex_unlock(request_queue_mutex);
-
- if (NULL == request) {
- SIMIX_process_suspend(self);
- } else {
-
- SIMIX_mutex_lock(request->mutex);
-
- // copy request to appropriate received queue
- message = xbt_mallocator_get(smpi_global->message_mallocator);
- message->comm = request->comm;
- message->src = request->src;
- message->dst = request->dst;
- message->tag = request->tag;
- message->buf = xbt_malloc(request->datatype->size * request->count);
- memcpy(message->buf, request->buf, request->datatype->size * request->count);
-
- dhost = request->comm->hosts[request->dst];
- drank = smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, dhost);
-
- SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[drank]);
- xbt_fifo_push(smpi_global->received_message_queues[drank], message);
- SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[drank]);
-
- request->completed = 1;
-
- communicate_action = SIMIX_action_communicate(shost, dhost,
- NULL, request->datatype->size * request->count * 1.0, -1.0);
-
- SIMIX_register_action_to_condition(communicate_action, request->cond);
- SIMIX_cond_wait(request->cond, request->mutex);
- SIMIX_unregister_action_to_condition(communicate_action, request->cond);
-
- SIMIX_mutex_unlock(request->mutex);
-
- // wake up receiver if necessary
- receiver_process = smpi_global->receiver_processes[drank];
-
- if (SIMIX_process_is_suspended(receiver_process)) {
- SIMIX_process_resume(receiver_process);
- }
-
- }
-
- SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
- running_hosts_count = smpi_global->running_hosts_count;
- SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
-
- } while (0 < running_hosts_count);
-
- SIMIX_mutex_lock(smpi_global->start_stop_mutex);
- smpi_global->ready_process_count--;
- if (smpi_global->ready_process_count == 0) {
- SIMIX_cond_broadcast(smpi_global->start_stop_cond);
- } else if (smpi_global->ready_process_count < 0) {
- // FIXME: can't happen! abort!
- }
- SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
-
- return 0;
-}
-
-int smpi_receiver(int argc, char **argv)
-{
- smx_process_t self;
- int rank;
-
- xbt_fifo_t request_queue;
- smx_mutex_t request_queue_mutex;
- xbt_fifo_t message_queue;
- smx_mutex_t message_queue_mutex;
- int size;
-
- int running_hosts_count;
-
- smpi_mpi_request_t *request;
- smpi_received_message_t *message;
-
- xbt_fifo_item_t request_item;
- xbt_fifo_item_t message_item;
-
- self = SIMIX_process_self();
- rank = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
-
- // make sure root is done before own initialization
- SIMIX_mutex_lock(smpi_global->start_stop_mutex);
- if (!smpi_global->root_ready) {
- SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
- }
- SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
-
- request_queue = smpi_global->pending_recv_request_queues[rank];
- request_queue_mutex = smpi_global->pending_recv_request_queues_mutexes[rank];
- message_queue = smpi_global->received_message_queues[rank];
- message_queue_mutex = smpi_global->received_message_queues_mutexes[rank];
- size = smpi_mpi_comm_size(smpi_mpi_global->mpi_comm_world);
-
- smpi_global->receiver_processes[rank] = self;
-
- // wait for all nodes to signal initializatin complete
- SIMIX_mutex_lock(smpi_global->start_stop_mutex);
- smpi_global->ready_process_count++;
- if (smpi_global->ready_process_count < 3 * size) {
- SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
- } else {
- SIMIX_cond_broadcast(smpi_global->start_stop_cond);
- }
- SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
-
- do {
- request = NULL;
- message = NULL;
-
- // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
-
- // FIXME: not the best way to request multiple locks...
- SIMIX_mutex_lock(request_queue_mutex);
- SIMIX_mutex_lock(message_queue_mutex);
- for (request_item = xbt_fifo_get_first_item(request_queue);
- NULL != request_item;
- request_item = xbt_fifo_get_next_item(request_item)) {
- request = xbt_fifo_get_item_content(request_item);
- for (message_item = xbt_fifo_get_first_item(message_queue);
- NULL != message_item;
- message_item = xbt_fifo_get_next_item(message_item)) {
- message = xbt_fifo_get_item_content(message_item);
- if (request->comm == message->comm &&
- (MPI_ANY_SOURCE == request->src || request->src == message->src) &&
- request->tag == message->tag) {
- xbt_fifo_remove_item(request_queue, request_item);
- xbt_fifo_remove_item(message_queue, message_item);
- goto stopsearch;