static unsigned int Get_GlobalOP_RecvSize (event_t *current, int is_root)
{
	unsigned int res = 0;
	switch (Get_EvEvent(current))
	{
		case MPI_BARRIER_EV:
		case MPI_IBARRIER_EV:
			res = 0;
		break;
		case MPI_REDUCE_EV:
		case MPI_IREDUCE_EV:
			res = (!is_root)?0:Get_EvSize(current);
		break;
		case MPI_BCAST_EV:
		case MPI_IBCAST_EV:
			res = (is_root)?0:Get_EvSize(current);
		break;
		case MPI_REDUCESCAT_EV:
		case MPI_IREDUCESCAT_EV:
			res = (is_root)?Get_EvSize(current):Get_EvAux(current);
		break;
		case MPI_SCAN_EV:
		case MPI_ISCAN_EV:
		case MPI_ALLREDUCE_EV:
		case MPI_IALLREDUCE_EV:
			res = Get_EvSize(current);
		break;
		default:
			res = Get_EvAux(current);
		break;
	}
	return res;
}
static int ANY_Send_Event (event_t * current, unsigned long long current_time,
	unsigned int cpu, unsigned int ptask, unsigned int task,
	unsigned int thread, FileSet_t *fset)
{
	thread_t * thread_info = GET_THREAD_INFO(ptask, task, thread);
	int tipus;
	int isimmediate;
	int comunicador;
	UINT64 valor;
	double temps;

	UNREFERENCED_PARAMETER(cpu);

	if (MPI_IBSEND_EV == Get_EvEvent(current) 
	    || MPI_ISSEND_EV == Get_EvEvent(current)
	    || MPI_IRSEND_EV == Get_EvEvent(current)
	    || MPI_ISEND_EV == Get_EvEvent(current))
	   	isimmediate = TRUE;
	else
		isimmediate = FALSE;

	temps = current_time-thread_info->Previous_Event_Time;
	temps /= 1000000000.0f;

	comunicador = alies_comunicador (Get_EvComm(current), 1, task);
	
	switch (Get_EvValue(current)) {
		case EVT_BEGIN:
			Dimemas_CPU_Burst (fset->output_file, task-1, thread-1, temps);
		break;

		case EVT_END: 
			if (Get_EvTarget(current) != MPI_PROC_NULL)
			{
#ifdef CPUZERO
				Dimemas_CPU_Burst (fset->output_file, task-1, thread-1, 0);
#endif
				if (!isimmediate)
					Dimemas_NX_BlockingSend (fset->output_file, task-1, thread-1, Get_EvTarget(current),
					  comunicador, Get_EvSize(current), Get_EvTag(current));
				else
					Dimemas_NX_ImmediateSend (fset->output_file, task-1, thread-1, Get_EvTarget(current),
					  comunicador, Get_EvSize(current), Get_EvTag(current));
			}
#ifdef CPUZERO
			Dimemas_CPU_Burst (fset->output_file, task-1, thread-1, 0);
#endif
		break;
	}

	Translate_MPI_MPIT2PRV (Get_EvEvent(current), Get_EvValue(current), &tipus, &valor);
	Dimemas_User_Event (fset->output_file, task-1, thread-1, tipus, valor);
	return 0;
}
void trace_communicationAt (unsigned ptask, unsigned task_s, unsigned thread_s, unsigned vthread_s,
	unsigned task_r, unsigned thread_r, unsigned vthread_r, event_t *send_begin,
	event_t *send_end, event_t *recv_begin, event_t *recv_end, 
	int atposition, off_t position)
{
	thread_t *thread_r_info, *thread_s_info;
	unsigned long long log_s, log_r, phy_s, phy_r;
	unsigned cpu_r, cpu_s;

	/* Look for the receive partner ... in the sender events */
	thread_r_info = GET_THREAD_INFO(ptask, task_r, thread_r);
	cpu_r = thread_r_info->cpu;

	/* Look for the sender partner ... in the receiver events */
	thread_s_info = GET_THREAD_INFO(ptask, task_s, thread_s);
	cpu_s = thread_s_info->cpu;

	/* Synchronize event times */
	log_s = TIMESYNC(ptask-1, task_s-1, Get_EvTime (send_begin));
	phy_s = TIMESYNC(ptask-1, task_s-1, Get_EvTime (send_end));
	log_r = TIMESYNC(ptask-1, task_r-1, Get_EvTime (recv_begin));
	phy_r = TIMESYNC(ptask-1, task_r-1, Get_EvTime (recv_end));

#if defined(DEBUG)
	fprintf (stderr, "trace_communicationAt: %u.%u.%u -> %u.%u.%u atposition=%d position=%llu\n",
	  ptask, task_s, thread_s, ptask, task_r, thread_r, atposition, position);
#endif

	trace_paraver_communication (cpu_s, ptask, task_s, thread_s, vthread_s, log_s, phy_s,
	  cpu_r, ptask, task_r, thread_r, vthread_r, log_r, phy_r, Get_EvSize (recv_end),
		Get_EvTag (recv_end), atposition, position);
}
static UINT64 Get_GlobalOP_RecvSize (event_t *current)
{
	UINT64 res;

	switch (Get_EvEvent(current))
	{
		case MPI_REDUCE_EV:
			if (Get_EvTag(current) == Get_EvAux(current))
				res = Get_EvSize(current);
			else
				res = 0;
		break;

		case MPI_BCAST_EV:
			if (Get_EvTag(current) != Get_EvTarget(current))
				res = Get_EvSize(current);
			else
				res = 0;
		break;

		case MPI_REDUCESCAT_EV:
		case MPI_SCAN_EV:
		case MPI_ALLREDUCE_EV:
			res = Get_EvSize(current);
		break;

		case MPI_ALLGATHER_EV:
		case MPI_ALLGATHERV_EV:
		case MPI_GATHER_EV:
		case MPI_GATHERV_EV:
		case MPI_SCATTER_EV:
		case MPI_SCATTERV_EV:
			res = Get_EvAux(current);
		break;

		case MPI_ALLTOALL_EV:
		case MPI_ALLTOALLV_EV:
			res = Get_EvTarget(current);
		break;

		case MPI_BARRIER_EV:
		default:
			res = 0;
		break;
	}
	return res;
}
int trace_pending_communication (unsigned int ptask, unsigned int task,
	unsigned int thread, unsigned vthread, event_t * begin_s, event_t * end_s, unsigned int recvr)
{
	unsigned long long log_s, phy_s;

	/* Synchronize event times */
	log_s = TIMESYNC (ptask-1, task-1, Get_EvTime (begin_s));
	phy_s = TIMESYNC (ptask-1, task-1, Get_EvTime (end_s));

	trace_paraver_pending_communication (task, ptask, task, thread, vthread, log_s,
		phy_s, recvr + 1, ptask, recvr + 1, thread /* 1? */ , thread /*vthread_r?*/,
		0ULL, 0ULL, Get_EvSize (begin_s), Get_EvTag (begin_s));
  return 0;
}
static int Any_Incoming_OPENSHMEM_Event (event_t * current_event,
        unsigned long long current_time, unsigned int cpu, unsigned int ptask,
        unsigned int task, unsigned int thread, FileSet_t *fset)
{
        UNREFERENCED_PARAMETER(fset);
        unsigned int  EvType  = Get_EvEvent(current_event);
        unsigned long EvValue = ((Get_EvValue (current_event) > 0) ? EvType - OPENSHMEM_BASE_EVENT + 1 : 0);
	unsigned int  EvSize  = Get_EvSize  (current_event);

        Switch_State (Get_State(EvType), (EvValue != EVT_END), ptask, task, thread);

        trace_paraver_state (cpu, ptask, task, thread, current_time);
        trace_paraver_event (cpu, ptask, task, thread, current_time, OPENSHMEM_BASE_EVENT, EvValue);
        trace_paraver_event (cpu, ptask, task, thread, current_time, OPENSHMEM_RECVBYTES_EV, EvSize);

	return 0;
}
/******************************************************************************
 ***  PersistentRequest_Event
 ******************************************************************************/
static int PersistentRequest_Event (event_t * current,
	unsigned long long current_time, unsigned int cpu, unsigned int ptask,
	unsigned int task, unsigned int thread, FileSet_t *fset)
{
	int comunicador;

	UNREFERENCED_PARAMETER(current_time);
	UNREFERENCED_PARAMETER(cpu);
	UNREFERENCED_PARAMETER(ptask);

	comunicador = alies_comunicador (Get_EvComm(current), 1, task);

	if (Get_EvTarget(current) == MPI_PROC_NULL)
		return 0;

#ifdef CPUZERO
	Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0);
#endif

	switch (Get_EvValue(current))
	{ 
		case MPI_IRECV_EV:
			Dimemas_NX_Irecv (fset->output_file, task-1, thread-1, Get_EvTarget(current), comunicador, Get_EvSize(current), Get_EvTag(current) );
		break;

		case MPI_ISEND_EV:
		case MPI_IBSEND_EV:
		case MPI_IRSEND_EV:
		case MPI_ISSEND_EV:
			if (MPI_PROC_NULL != Get_EvTarget(current))
			{
#ifdef CPUZERO
				Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0);
#endif
				Dimemas_NX_ImmediateSend(fset->output_file, task-1, thread-1, Get_EvTarget(current),
				  comunicador, Get_EvSize(current), Get_EvTag(current));
			}
			break;
		default:
			break;
	}
  
	return 0;
}
static int GlobalOP_event (event_t * current_event,
	unsigned long long current_time, unsigned int cpu, unsigned int ptask,
	unsigned int task, unsigned int thread, FileSet_t *fset)
{
	unsigned int EvType, EvValue;
	unsigned int comm_id, send_size, receive_size, is_root;
	UNREFERENCED_PARAMETER(fset);

	EvType  = Get_EvEvent (current_event);
	EvValue = Get_EvValue (current_event);

	/* First global operation found, start matching communications from now on */
	if ((tracingCircularBuffer())                                  && /* Circular buffer is enabled */ 
            (getBehaviourForCircularBuffer() == CIRCULAR_SKIP_MATCHES) && /* The buffer behavior is to skip matches */
            (!MatchComms_Enabled(ptask, task))                         && /* Not matching already */
            (EvValue == EVT_END)                                       && /* End of the collective */
            (Get_EvSize(current_event) == GET_NUM_TASKS(ptask))           /* World collective */
            /* (getTagForCircularBuffer() == Get_EvAux(current_event)) */)
	{
		MatchComms_On(ptask, task);
	}

	Switch_State (Get_State(EvType), (EvValue == EVT_BEGIN), ptask, task, thread);

	trace_paraver_state (cpu, ptask, task, thread, current_time);
	trace_paraver_event (cpu, ptask, task, thread, current_time, EvType, EvValue);

	if (EVT_BEGIN == EvValue)
	{
		comm_id = Get_GlobalOP_CommID (current_event);
		is_root = Get_GlobalOP_isRoot (current_event, task);
		send_size = Get_GlobalOP_SendSize (current_event, is_root);
		receive_size = Get_GlobalOP_RecvSize (current_event, is_root);
		trace_enter_global_op (cpu, ptask, task, thread, current_time, comm_id,
		  send_size, receive_size, is_root?1:0);

		Enable_MPI_Soft_Counter (EvType);
	}

	return 0;
}
static int Other_MPI_Event (event_t * current_event, 
	unsigned long long current_time, unsigned int cpu, unsigned int ptask, 
	unsigned int task, unsigned int thread, FileSet_t *fset)
{
	unsigned int EvType, EvValue;
	UNREFERENCED_PARAMETER(fset);

	EvType  = Get_EvEvent (current_event);
	EvValue = Get_EvValue (current_event);

	Switch_State (Get_State(EvType), (EvValue == EVT_BEGIN), ptask, task, thread);

	/* XXX: Workaround to set the state to NOT_TRACING after the MPI_Init when using circular buffer.
     * We should definitely do this another way round. 
	 */ 
	if ((EvType == MPI_INIT_EV) && (EvValue == EVT_END))
	{
		if ((tracingCircularBuffer()) && (getBehaviourForCircularBuffer() == CIRCULAR_SKIP_MATCHES))
		{
			/* The first event beyond the MPI_Init will remove the STATE_NOT_TRACING (see Push_State) */
			Push_State (STATE_NOT_TRACING, ptask, task, thread);
		}
	}

	trace_paraver_state (cpu, ptask, task, thread, current_time);
	trace_paraver_event (cpu, ptask, task, thread, current_time, EvType, EvValue);

	if (EvType == MPI_INIT_EV && EvValue == EVT_BEGIN)
	{
		UINT32 PID = Get_EvTarget (current_event);
		UINT32 PPID = Get_EvSize (current_event);
		UINT32 Depth = Get_EvTag (current_event);
		trace_paraver_event (cpu, ptask, task, thread, current_time, PID_EV, PID);
		trace_paraver_event (cpu, ptask, task, thread, current_time, PPID_EV, PPID);
		trace_paraver_event (cpu, ptask, task, thread, current_time, FORK_DEPTH_EV, Depth);
	}

	return 0;
}
int MPI_PersistentRequest_Event (event_t * current_event,
	unsigned long long current_time, unsigned int cpu, unsigned int ptask,
	unsigned int task, unsigned int thread, FileSet_t *fset)
{
	thread_t *thread_info;
	task_t *task_info, *task_info_partner;
	event_t *recv_begin, *recv_end;
	event_t *send_begin, *send_end;
	off_t send_position;
	unsigned recv_thread, send_thread, recv_vthread, send_vthread;
	int EvComm; 

	EvComm = Get_EvComm( current_event );

	thread_info = GET_THREAD_INFO(ptask, task, thread);
	task_info = GET_TASK_INFO(ptask, task);
	trace_paraver_state (cpu, ptask, task, thread, current_time);

	/* If this is a send, look for the receive */
	if (Get_EvValue (current_event) == MPI_ISEND_EV)
	{
		thread_info->Send_Rec = current_event;

		if (MatchComms_Enabled(ptask, task))
		{
			if (MPI_PROC_NULL != Get_EvTarget (current_event))
			{
				int target_ptask = intercommunicators_get_target_ptask( ptask, task, EvComm );

				if (isTaskInMyGroup (fset, target_ptask-1, Get_EvTarget(current_event)))
				{
#if defined(DEBUG)
					fprintf (stderr, "PERS_REQ_ISEND_CMD(%u): TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d\n", Get_EvValue (current_event), current_time, Get_EvTime(current_event), task-1, Get_EvTarget(current_event), Get_EvTag(current_event));
#endif

					task_info_partner = GET_TASK_INFO(target_ptask, Get_EvTarget(current_event)+1);

					CommunicationQueues_ExtractRecv (task_info_partner->recv_queue, task-1, Get_EvTag (current_event), &recv_begin, &recv_end, &recv_thread, &recv_vthread, 0);

					if (recv_begin == NULL || recv_end == NULL)
					{
						off_t position;
#if defined(DEBUG)
						fprintf (stderr, "PER_REQ_ISEND_CMD DID NOT find a partner\n");
#endif
						position = WriteFileBuffer_getPosition (thread_info->file->wfb);
						CommunicationQueues_QueueSend (task_info->send_queue, current_event, current_event, position, thread, thread_info->virtual_thread, Get_EvTarget(current_event), Get_EvTag(current_event), 0);
						trace_paraver_unmatched_communication (1, ptask, task, thread, thread_info->virtual_thread, current_time, Get_EvTime(current_event), 1, target_ptask, Get_EvTarget(current_event)+1, 1, Get_EvSize(current_event), Get_EvTag(current_event));
					}
					else
					{
#if defined(DEBUG)
						fprintf (stderr, "PER_REQ_ISEND_CMD DID NOT find a partner\n");
#endif
						trace_communicationAt (ptask, task, thread, thread_info->virtual_thread, target_ptask, 1+Get_EvTarget(current_event), recv_thread, recv_vthread, current_event, current_event, recv_begin, recv_end, FALSE, 0);
					}

				}
#if defined(PARALLEL_MERGE)
				else
				{
#if defined(DEBUG)
					fprintf (stdout, "SEND_CMD(%u): TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d >> PENDING\n", Get_EvEvent(current_event), current_time, Get_EvTime(current_event), task-1, Get_EvTarget(current_event), Get_EvTag(current_event));
#endif
					trace_pending_communication (ptask, task, thread, thread_info->virtual_thread, thread_info->Send_Rec, current_event, target_ptask, Get_EvTarget (current_event));
				}
#endif
			}
		}
	}

	/* If this is a receive, look for the send */
	if (Get_EvValue(current_event) == MPI_IRECV_EV)
	{
		thread_info->Recv_Rec = current_event;

		if (MatchComms_Enabled(ptask, task))
		{
			event_t *receive = Search_MPI_IRECVED (current_event, Get_EvAux (current_event), thread_info->file);
			if (NULL != receive)
			{
				int target_ptask = intercommunicators_get_target_ptask( ptask, task, EvComm );

				if (MPI_PROC_NULL != Get_EvTarget(receive))
				{
					if (isTaskInMyGroup (fset, target_ptask-1, Get_EvTarget(receive)))
					{
#if defined(DEBUG)
						fprintf (stderr, "PERS_REQ_IRECV_CMD(%u): TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d\n", Get_EvValue (current_event), current_time, Get_EvTime(current_event), task-1, Get_EvTarget(receive), Get_EvTag(receive));
#endif

						task_info_partner = GET_TASK_INFO(ptask, Get_EvTarget(receive)+1);

						CommunicationQueues_ExtractSend (task_info_partner->send_queue, task-1, Get_EvTag (receive), &send_begin, &send_end, &send_position, &send_thread, &send_vthread, 0);

						if (NULL == send_begin || NULL == send_end)
						{
#if defined(DEBUG)
							fprintf (stderr, "PER_REQ_IRECV_CMD DID NOT find a partner\n");
#endif
							CommunicationQueues_QueueRecv (task_info->recv_queue, current_event, receive, thread, thread_info->virtual_thread, Get_EvTarget(current_event), Get_EvTag(current_event), 0);
						}
						else if (NULL != send_begin && NULL != send_end)
						{
#if defined(DEBUG)
							fprintf (stderr, "PERS_REQ_IRECV_CMD find partner (send position = %llu)\n", (unsigned long long) send_position);
#endif
							trace_communicationAt (target_ptask, 1+Get_EvTarget(receive), send_thread, send_vthread, ptask, task, thread, thread_info->virtual_thread, send_begin, send_end, current_event, receive, TRUE, send_position);
						}
						else
							fprintf (stderr, "mpi2prv: Attention CommunicationQueues_ExtractSend returned send_begin = %p and send_end = %p\n", send_begin, send_end);
					}
#if defined(PARALLEL_MERGE)
					else
					{
						UINT64 log_r, phy_r;

						log_r = TIMESYNC (ptask-1, task-1, Get_EvTime(current_event));
						phy_r = TIMESYNC (ptask-1, task-1, Get_EvTime(receive));
						AddForeignRecv (phy_r, log_r, Get_EvTag(receive), ptask-1, task-1, thread-1,
							thread_info->virtual_thread-1, target_ptask-1, Get_EvTarget(receive), fset, MatchComms_GetZone(ptask, task));
					}
#endif
				}
			}
		}
	}

	return 0;
}
static int SendRecv_Event (event_t * current_event, 
	unsigned long long current_time, unsigned int cpu, unsigned int ptask, 
	unsigned int task, unsigned int thread, FileSet_t *fset)
{
	thread_t *thread_info;
	task_t *task_info, *task_info_partner;
#if !defined(AVOID_SENDRECV)
	unsigned recv_thread, send_thread, recv_vthread, send_vthread;
	event_t *recv_begin, *recv_end, *send_begin, *send_end;
	off_t send_position;
#endif
	int EvComm = Get_EvComm (current_event);

	Switch_State (STATE_SENDRECVOP, (Get_EvValue(current_event) == EVT_BEGIN), ptask, task, thread);

	trace_paraver_state (cpu, ptask, task, thread, current_time);
	trace_paraver_event (cpu, ptask, task, thread, current_time, Get_EvEvent(current_event), Get_EvValue(current_event));

	thread_info = GET_THREAD_INFO(ptask, task, thread);
	task_info = GET_TASK_INFO(ptask, task);

	if (!get_option_merge_SkipSendRecvComms())
	{
		if (Get_EvValue (current_event) == EVT_BEGIN)
		{
			thread_info->Send_Rec = current_event;

			/* Treat the send part */
			if (MatchComms_Enabled(ptask, task))
				if (MPI_PROC_NULL != Get_EvTarget (thread_info->Send_Rec))
				{
					int target_ptask = intercommunicators_get_target_ptask( ptask, task, EvComm );

					if (isTaskInMyGroup (fset, target_ptask-1, Get_EvTarget(thread_info->Send_Rec)))
					{
#if defined(DEBUG)
						fprintf (stderr, "SENDRECV/SEND: TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d\n", current_time, Get_EvTime(thread_info->Send_Rec), task-1, Get_EvTarget(thread_info->Send_Rec), Get_EvTag(thread_info->Send_Rec));
#endif
						task_info_partner = GET_TASK_INFO(target_ptask, Get_EvTarget(thread_info->Send_Rec)+1);

						CommunicationQueues_ExtractRecv (task_info_partner->recv_queue, task-1, Get_EvTag (thread_info->Send_Rec), &recv_begin, &recv_end, &recv_thread, &recv_vthread, 0);

						if (recv_begin == NULL || recv_end == NULL)
						{
							off_t position;

#if defined(DEBUG)
							fprintf (stderr, "SENDRECV/SEND DID NOT find partner\n");
#endif
							position = WriteFileBuffer_getPosition (thread_info->file->wfb);
							CommunicationQueues_QueueSend (task_info->send_queue, thread_info->Send_Rec, current_event, position, thread, thread_info->virtual_thread, Get_EvTarget(thread_info->Send_Rec), Get_EvTag(thread_info->Send_Rec), 0);
							trace_paraver_unmatched_communication (1, ptask, task, thread, thread_info->virtual_thread, current_time, Get_EvTime(current_event), 1, target_ptask, Get_EvTarget(current_event)+1, 1, Get_EvSize(current_event), Get_EvTag(current_event));
						}
						else if (recv_begin != NULL && recv_end != NULL)
						{
#if defined(DEBUG)
							fprintf (stderr, "SENDRECV/SEND found partner\n");
#endif
							trace_communicationAt (ptask, task, thread, thread_info->virtual_thread, target_ptask, 1+Get_EvTarget(thread_info->Send_Rec), recv_thread, recv_vthread, thread_info->Send_Rec, current_event, recv_begin, recv_end, FALSE, 0);
						}
						else
							fprintf (stderr, "mpi2prv: Attention CommunicationQueues_ExtractRecv returned recv_begin = %p and recv_end = %p\n", recv_begin, recv_end);
					}
#if defined(PARALLEL_MERGE)
					else
					{
#if defined(DEBUG)
						fprintf (stdout, "SEND_CMD(%u): TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d >> PENDING\n", Get_EvEvent(current_event), current_time, Get_EvTime(current_event), task-1, Get_EvTarget(current_event), Get_EvTag(current_event));
#endif
						trace_pending_communication (ptask, task, thread, thread_info->virtual_thread, thread_info->Send_Rec, current_event, target_ptask, Get_EvTarget (thread_info->Send_Rec));
					}
#endif /* PARALLEL_MERGE */
					}

		}
		else if (Get_EvValue (current_event) == EVT_END)
		{
			thread_info->Recv_Rec = current_event;

			/* Treat the receive part */
			if (MatchComms_Enabled(ptask, task))
				if (MPI_PROC_NULL != Get_EvTarget (thread_info->Recv_Rec))
				{
					int target_ptask = intercommunicators_get_target_ptask( ptask, task, EvComm );

					if (isTaskInMyGroup (fset, target_ptask-1, Get_EvTarget(thread_info->Recv_Rec)))
					{
#if defined(DEBUG)
						fprintf (stderr, "SENDRECV/RECV: TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d\n", current_time, Get_EvTime(thread_info->Recv_Rec), task-1, Get_EvTarget(thread_info->Recv_Rec), Get_EvTag(thread_info->Recv_Rec));
#endif

						task_info_partner = GET_TASK_INFO(target_ptask, Get_EvTarget(thread_info->Recv_Rec)+1);

						CommunicationQueues_ExtractSend (task_info_partner->send_queue, task-1, Get_EvTag (thread_info->Recv_Rec), &send_begin, &send_end, &send_position, &send_thread, &send_vthread, 0);

						if (NULL == send_begin && NULL == send_end)
						{
#if defined(DEBUG)
							fprintf (stderr, "SENDRECV/RECV DID NOT find partner\n");
#endif
							CommunicationQueues_QueueRecv (task_info->recv_queue, thread_info->Send_Rec, current_event, thread, thread_info->virtual_thread, Get_EvTarget(thread_info->Recv_Rec), Get_EvTag(thread_info->Recv_Rec), 0);
						}
						else if (NULL != send_begin && NULL != send_end)
						{
#if defined(DEBUG)
							fprintf (stderr, "SENDRECV/RECV found partner\n");
#endif
							trace_communicationAt (target_ptask, 1+Get_EvTarget(thread_info->Recv_Rec), send_thread, send_vthread, ptask, task, thread, thread_info->virtual_thread, send_begin, send_end, thread_info->Send_Rec, thread_info->Recv_Rec, TRUE, send_position);
						}
						else
							fprintf (stderr, "mpi2prv: Attention CommunicationQueues_ExtractSend returned send_begin = %p and send_end = %p\n", send_begin, send_end);
					}
#if defined(PARALLEL_MERGE)
					else
					{
						UINT64 log_r, phy_r;

						log_r = TIMESYNC (ptask-1, task-1, Get_EvTime(thread_info->Send_Rec));
						phy_r = TIMESYNC (ptask-1, task-1, Get_EvTime(current_event));
						AddForeignRecv (phy_r, log_r, Get_EvTag(current_event), ptask-1, task-1, thread-1,
							thread_info->virtual_thread-1, target_ptask-1, Get_EvTarget(current_event), fset, MatchComms_GetZone(ptask, task));
					}
#endif /* PARALLEL_MERGE */
				}
		}
	}

	return 0;
}
static int Any_Send_Event (event_t * current_event,
	unsigned long long current_time, unsigned int cpu, unsigned int ptask,
	unsigned int task, unsigned int thread, FileSet_t *fset)
{
	unsigned recv_thread, recv_vthread, EvType, EvValue;
	thread_t *thread_info;
	task_t *task_info, *task_info_partner;
	event_t * recv_begin, * recv_end;
	int EvComm;

	EvType  = Get_EvEvent(current_event);
	EvValue = Get_EvValue(current_event);
	EvComm  = Get_EvComm (current_event);

	Switch_State (Get_State(EvType), (EvValue == EVT_BEGIN), ptask, task, thread);

	trace_paraver_state (cpu, ptask, task, thread, current_time);
	trace_paraver_event (cpu, ptask, task, thread, current_time, EvType, EvValue);

	thread_info = GET_THREAD_INFO(ptask, task, thread);
	task_info = GET_TASK_INFO(ptask, task);

	switch (EvValue)
	{
		case EVT_BEGIN:
			thread_info->Send_Rec = current_event;
		break;
		case EVT_END:
			if (MatchComms_Enabled(ptask, task))
			{
				if (MPI_PROC_NULL != Get_EvTarget (current_event))
				{
					int target_ptask = intercommunicators_get_target_ptask( ptask, task, EvComm );

					if (isTaskInMyGroup (fset, target_ptask-1, Get_EvTarget(current_event)))
					{
#if defined(DEBUG)
						fprintf (stderr, "SEND_CMD(%u): TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d\n", EvType, current_time, Get_EvTime(current_event), task-1, Get_EvTarget(current_event), Get_EvTag(current_event));
#endif
						task_info_partner = GET_TASK_INFO(target_ptask, Get_EvTarget(current_event)+1);
						CommunicationQueues_ExtractRecv (task_info_partner->recv_queue, task-1, Get_EvTag (current_event), &recv_begin, &recv_end, &recv_thread, &recv_vthread, 0);

						if (recv_begin == NULL || recv_end == NULL)
						{
							off_t position;

#if defined(DEBUG)
							fprintf (stderr, "SEND_CMD(%u) DID NOT find receiver\n", EvType);
#endif
							position = WriteFileBuffer_getPosition (thread_info->file->wfb);
							CommunicationQueues_QueueSend (task_info->send_queue, thread_info->Send_Rec, current_event, position, thread, thread_info->virtual_thread, Get_EvTarget(current_event), Get_EvTag(current_event), 0);
							trace_paraver_unmatched_communication (1, ptask, task, thread, thread_info->virtual_thread, current_time, Get_EvTime(current_event), 1, target_ptask, Get_EvTarget(current_event)+1, 1, Get_EvSize(current_event), Get_EvTag(current_event));
						}
						else
						{
#if defined(DEBUG)
							fprintf (stderr, "SEND_CMD(%u) find receiver\n", EvType);
#endif
							trace_communicationAt (ptask, task, thread, thread_info->virtual_thread, target_ptask, 1+Get_EvTarget(current_event), recv_thread, recv_vthread, thread_info->Send_Rec, current_event, recv_begin, recv_end, FALSE, 0);
						}
					}
#if defined(PARALLEL_MERGE)
					else
					{
#if defined(DEBUG)
						fprintf (stdout, "SEND_CMD(%u): TIME/TIMESTAMP %lld/%lld IAM %d PARTNER %d tag %d >> PENDING\n", Get_EvEvent(current_event), current_time, Get_EvTime(current_event), task-1, Get_EvTarget(current_event), Get_EvTag(current_event));
#endif
						trace_pending_communication (ptask, task, thread, thread_info->virtual_thread, thread_info->Send_Rec, current_event, target_ptask, Get_EvTarget (current_event));
					}
#endif
				}
			}
		break;
	}
	return 0;
}
Exemple #13
0
/******************************************************************************
 ***  GenerateAliesComunicator
 ******************************************************************************/
int GenerateAliesComunicator (
   event_t * current_event, unsigned long long current_time, unsigned int cpu,
   unsigned int ptask, unsigned int task, unsigned int thread, FileSet_t * fset,
   unsigned long long *num_events, int traceformat)
{
	unsigned int i = 0;
	unsigned int EvValue = Get_EvValue (current_event);
	unsigned int EvCommType = Get_EvTarget (current_event);
	unsigned int EvType = Get_EvEvent (current_event);

	if (EvValue == EVT_BEGIN)
	{
#if defined(DEBUG_COMMUNICATORS)
		fprintf (stderr, "DEBUG: new communicator definition (commtype = %d)\n",
			EvCommType);
#endif

		if (PRV_SEMANTICS == traceformat)
			if (Get_EvAux (current_event)) /* Shall we emit this into tracefile? */ 
			{
				trace_paraver_state (cpu, ptask, task, thread, current_time);
				trace_paraver_event (cpu, ptask, task, thread, current_time, EvType, EVT_BEGIN);
			}

		/* Build COMM WORLD communicator */
		if (MPI_COMM_WORLD_ALIAS == EvCommType)
		{
			TipusComunicador new_comm;
			unsigned int i;

#if defined(DEBUG_COMMUNICATORS)
			fprintf (stderr, "DEBUG: defining MPI_COMM_WORLD (%d members)\n", Get_EvSize (current_event));
#endif

			new_comm.id = Get_EvComm (current_event);
			new_comm.num_tasks = Get_EvSize (current_event);
			new_comm.tasks = (int*) malloc(sizeof(int)*new_comm.num_tasks);
			if (NULL == new_comm.tasks)
			{
				fprintf (stderr, "mpi2prv: Can't allocate memory for a COMM WORLD alias\n");
				fflush (stderr);
				exit (-1);
			}
			for (i = 0; i < new_comm.num_tasks; i++)
				new_comm.tasks[i] = i;
#if defined(PARALLEL_MERGE)
			ParallelMerge_AddIntraCommunicator (ptask, task, MPI_COMM_WORLD_ALIAS, new_comm.id, new_comm.num_tasks, new_comm.tasks);
#else
			afegir_comunicador (&new_comm, ptask, task);
#endif
			free (new_comm.tasks);
		}
		/* Build COMM SELF communicator */
		else if (MPI_COMM_SELF_ALIAS == EvCommType)
		{
			TipusComunicador new_comm;

#if defined(DEBUG_COMMUNICATORS)
			fprintf (stderr, "DEBUG: defining MPI_COMM_SELF (%d members)\n", Get_EvSize (current_event));
#endif

			new_comm.id = Get_EvComm (current_event);
			new_comm.num_tasks = 1;
			new_comm.tasks = (int*) malloc(sizeof(int)*new_comm.num_tasks);
			if (NULL == new_comm.tasks)
			{
				fprintf (stderr, "mpi2prv: Can't allocate memory for a COMM SELF alias\n");
				fflush (stderr);
				exit (-1);
			}
			new_comm.tasks[0] = task-1;
#if defined(PARALLEL_MERGE)
			ParallelMerge_AddIntraCommunicator (ptask, task, MPI_COMM_SELF_ALIAS, new_comm.id, new_comm.num_tasks, new_comm.tasks);
#else
			afegir_comunicador (&new_comm, ptask, task);
#endif
			free (new_comm.tasks);
		}
		else if (MPI_NEW_INTERCOMM_ALIAS == EvCommType)
		{
#if defined(DEBUG_COMMUNICATORS)
			fprintf (stderr, "DEBUG: defining new INTERCOMM\n");
#endif
			i = BuildInterCommunicatorFromFile (current_event, current_time, cpu, ptask, task, thread, fset);
		}
		else
		{
#if defined(DEBUG_COMMUNICATORS)
			fprintf (stderr, "DEBUG: defining new COMM (%d members id = %d)\n", Get_EvSize(current_event), Get_EvComm (current_event));
#endif

			i = BuildCommunicatorFromFile (current_event, current_time, cpu, ptask, task,
				thread, fset);
		}
	}
	else if (EvValue == EVT_END)
	{
		if (PRV_SEMANTICS == traceformat)
			if (Get_EvAux (current_event)) /* Shall we emit this into tracefile? */
				trace_paraver_event (cpu, ptask, task, thread, current_time, EvType, EVT_END);
	}

	*num_events = i+1;
	/* Count how many records have we processed
		(i communicator members + begin of communicator event) */
	return 0;
}
Exemple #14
0
/******************************************************************************
 ***  BuildCommunicatorFromFile
 ******************************************************************************/
static unsigned int BuildCommunicatorFromFile (event_t *current_event,
  unsigned long long current_time, unsigned int cpu, unsigned int ptask,
  unsigned int task, unsigned int thread, FileSet_t * fset)
{
	TipusComunicador new_comm;
	unsigned int i = 0;
	unsigned int foo;
	unsigned int EvType = Get_EvEvent (current_event);
	UNREFERENCED_PARAMETER(current_time);
	UNREFERENCED_PARAMETER(cpu);

	/* New communicator definition starts */
	new_comm.id = Get_EvComm (current_event);
	new_comm.num_tasks = Get_EvSize (current_event);
	new_comm.tasks = (int*) malloc(sizeof(int)*new_comm.num_tasks);
	if (NULL == new_comm.tasks)
	{
		fprintf (stderr, "mpi2prv: Can't allocate memory for a COMM SELF alias\n");
		fflush (stderr);
		exit (-1);
	}
#if defined(DEBUG_COMMUNICATORS)
	fprintf (stderr, "DEBUG: New comm: id=%lu, num_tasks=%u\n", new_comm.id, new_comm.num_tasks);
#endif

	/* Process each communicator member */
	current_event = GetNextEvent_FS (fset, &foo, &ptask, &task, &thread);
	if (current_event != NULL)
		EvType = Get_EvEvent (current_event);

	while (i < new_comm.num_tasks && current_event != NULL && 
        (EvType == MPI_RANK_CREACIO_COMM_EV || EvType == FLUSH_EV))
	{
		if (EvType == MPI_RANK_CREACIO_COMM_EV)
		{
			/* Save this task as member of the communicator */
			new_comm.tasks[i] = Get_EvValue (current_event);
#if defined(DEBUG_COMMUNICATORS)
			fprintf (stderr, "  -- task %d\n", new_comm.tasks[i]);
#endif
			i++;
		}
		if (i < new_comm.num_tasks)
		{
			current_event = GetNextEvent_FS (fset, &foo, &ptask, &task, &thread);
			if (current_event != NULL)
				EvType = Get_EvEvent (current_event);
		}
	}

	/* End of communicator definition. Assign an alias for this communicator. */
	if (i != new_comm.num_tasks)
	{
		unsigned long long tmp_time = 0;
		if (current_event != NULL) tmp_time = Get_EvTime(current_event);
		fprintf (stderr, "mpi2prv: Error: Incorrect communicator definition! (%d out of %d definitions)\n"
			"EvType: %u, Time: %llu, ptask: %u, task: %u, thread: %u\n",
			i, new_comm.num_tasks, EvType, tmp_time, ptask, task, thread);
		exit (0);
	}
	else
	{
#if defined(PARALLEL_MERGE)
		ParallelMerge_AddIntraCommunicator (ptask, task, 0, new_comm.id,
		  new_comm.num_tasks, new_comm.tasks);
#else
		afegir_comunicador (&new_comm, ptask, task);
#endif
	}

	free (new_comm.tasks);

	return i;
}
/******************************************************************************
 ***  Sendrecv_Event
 ******************************************************************************/
static int SendRecv_Event (event_t * current, unsigned long long current_time,
	unsigned int cpu, unsigned int ptask, unsigned int task, unsigned int thread,
	FileSet_t *fset)
{
	static unsigned int receiver, send_tag;
	static int send_size;
	thread_t * thread_info = GET_THREAD_INFO(ptask, task, thread);
	UINT64 valor;
	unsigned int sender = 0;
	unsigned int receive_tag = 0;
	unsigned int receive_size = 0;
	int tipus;
	int comunicador;
	double temps;

	UNREFERENCED_PARAMETER(cpu);

	temps = current_time-thread_info->Previous_Event_Time;
	temps /= 1000000000.0f;

	comunicador = alies_comunicador (Get_EvComm(current), 1, task);
	
	switch( Get_EvValue(current) )
	{
		case EVT_BEGIN:
			Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, temps);
			receiver= Get_EvTarget(current);
			send_tag= Get_EvTag(current);
			send_size= Get_EvSize(current);
		break;

		case EVT_END:
#ifdef CPUZERO
			MPTRACE_ERROR(Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0));
#endif
			if (Get_EvTarget( current ) != MPI_PROC_NULL)
			{
				sender= Get_EvTarget(current);
				receive_tag= Get_EvTag(current);
				receive_size= Get_EvSize(current);                       

#ifdef CPUZERO
				Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0);
#endif
				Dimemas_NX_Irecv (fset->output_file, task-1, thread-1, sender, comunicador, receive_size, receive_tag);
			}

			if (receiver != MPI_PROC_NULL)
			{
#ifdef CPUZERO
				Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0);
#endif
				Dimemas_NX_BlockingSend (fset->output_file, task-1, thread-1, receiver, Get_EvComm(current), send_size, send_tag);
			}

			if (Get_EvTarget( current ) != MPI_PROC_NULL)
			{
#ifdef CPUZERO
				Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0);
#endif
				Dimemas_NX_Wait (fset->output_file, task-1, thread-1, sender, comunicador, receive_size, receive_tag);
			}
#ifdef CPUZERO
			Dimemas_CPU_Burst(fset->output_file, task-1, thread-1, 0);
#endif
		break;
	}

	Translate_MPI_MPIT2PRV (Get_EvEvent(current), Get_EvValue(current), &tipus, &valor);
	Dimemas_User_Event (fset->output_file, task-1, thread-1, tipus, valor);

	return 0;
}
/******************************************************************************
 ***  Irecved_Event
 ******************************************************************************/
static int Irecved_Event (event_t * current, unsigned long long current_time,
	unsigned int cpu, unsigned int ptask, unsigned int task, unsigned int thread,
	FileSet_t *fset)
{
	int comunicador;

	UNREFERENCED_PARAMETER(cpu);
	UNREFERENCED_PARAMETER(ptask);
	UNREFERENCED_PARAMETER(current_time);

	comunicador = alies_comunicador (Get_EvComm(current), 1, task);

	if (MPI_PROC_NULL != Get_EvTarget(current))
	{
#ifdef CPUZERO
		Dimemas_CPU_Burst (fset->output_file, task-1, thread-1, 0);
#endif
		Dimemas_NX_Wait (fset->output_file, task-1, thread-1, Get_EvTarget(current), comunicador, Get_EvSize(current), Get_EvTag(current));
	}

	return 0;
}