⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch3_progress_connect.c

📁 mpi并行计算的c++代码 可用vc或gcc编译通过 可以用来搭建并行计算试验环境
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* *  (C) 2001 by Argonne National Laboratory. *      See COPYRIGHT in top-level directory. */#include "ch3i_progress.h"volatile unsigned int MPIDI_CH3I_progress_completion_count = 0;MPIDI_CH3I_Connection_t * MPIDI_CH3I_listener_conn = NULL;/* local prototypes *//* static int MPIDI_CH3I_Shm_connect(MPIDI_VC_t *vc, char *business_card, int *flag); */#undef FUNCNAME#define FUNCNAME MPIDI_CH3_Connection_terminate#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)int MPIDI_CH3_Connection_terminate(MPIDI_VC_t * vc){    int mpi_errno = MPI_SUCCESS;    if (vc->ch.bShm)    {	/* There is no post_close for shm connections so handle them as closed immediately. */	MPIDI_CH3I_SHM_Remove_vc_references(vc);	MPIDI_CH3U_Handle_connection(vc, MPIDI_VC_EVENT_TERMINATED);    }    else    {	vc->ch.conn->state = CONN_STATE_CLOSING;	mpi_errno = MPIDU_Sock_post_close(vc->ch.sock);	if (mpi_errno != MPI_SUCCESS) {	    MPIU_ERR_SET(mpi_errno,MPI_ERR_OTHER, "**fail");	    goto fn_exit;	}    }  fn_exit:    return mpi_errno;}#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_SHM_Remove_vc_read_references#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)static void MPIDI_CH3I_SHM_Remove_vc_read_references(MPIDI_VC_t *vc){    MPIDI_VC_t *iter, *trailer;    /* remove vc from the reading list */    iter = trailer = MPIDI_CH3I_Process.shm_reading_list;    while (iter != NULL)    {	if (iter == vc)	{	    /*printf("[%s%d]removing read vc%d.state = %s (%s)\n", MPIU_DBG_parent_str, MPIR_Process.comm_world->rank, vc->pg_rank, VCState(vc), (vc->pg && vc->pg_rank >= 0) ? vc->pg->id : "?");fflush(stdout);*/	    /* Mark the connection as NOT read connected so it won't be mistaken for active */	    /* Since shm connections are uni-directional the following three states are considered active:	     * MPIDI_VC_STATE_ACTIVE + ch.shm_read_connected = 0 - active in the write direction	     * MPIDI_VC_STATE_INACTIVE + ch.shm_read_connected = 1 - active in the read direction	     * MPIDI_VC_STATE_ACTIVE + ch.shm_read_connected = 1 - active in both directions	     */	    vc->ch.shm_read_connected = 0;	    if (trailer != iter)	    {		/* remove the vc from the list */		trailer->ch.shm_next_reader = iter->ch.shm_next_reader;	    }	    else	    {		/* remove the vc from the head of the list */		MPIDI_CH3I_Process.shm_reading_list = MPIDI_CH3I_Process.shm_reading_list->ch.shm_next_reader;	    }	}	if (trailer != iter)	    trailer = trailer->ch.shm_next_reader;	iter = iter->ch.shm_next_reader;    }}#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_SHM_Remove_vc_write_references#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)static void MPIDI_CH3I_SHM_Remove_vc_write_references(MPIDI_VC_t *vc){    MPIDI_VC_t *iter, *trailer;    /* remove the vc from the writing list */    iter = trailer = MPIDI_CH3I_Process.shm_writing_list;    while (iter != NULL)    {	if (iter == vc)	{	    /*printf("[%s%d]removing write vc%d.state = %s (%s)\n", MPIU_DBG_parent_str, MPIR_Process.comm_world->rank, vc->pg_rank, VCState(vc), (vc->pg && vc->pg_rank >= 0) ? vc->pg->id : "?");fflush(stdout);*/	    if (trailer != iter)	    {		/* remove the vc from the list */		trailer->ch.shm_next_writer = iter->ch.shm_next_writer;	    }	    else	    {		/* remove the vc from the head of the list */		MPIDI_CH3I_Process.shm_writing_list = MPIDI_CH3I_Process.shm_writing_list->ch.shm_next_writer;	    }	}	if (trailer != iter)	    trailer = trailer->ch.shm_next_writer;	iter = iter->ch.shm_next_writer;    }}#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_SHM_Remove_vc_references#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)void MPIDI_CH3I_SHM_Remove_vc_references(MPIDI_VC_t *vc){    MPIDI_CH3I_SHM_Remove_vc_read_references(vc);    MPIDI_CH3I_SHM_Remove_vc_write_references(vc);}#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_SHM_Add_to_reader_list#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)void MPIDI_CH3I_SHM_Add_to_reader_list(MPIDI_VC_t *vc){    MPIDI_CH3I_SHM_Remove_vc_read_references(vc);    /*printf("[%s%d]adding read vc%d.state = %s (%s)\n", MPIU_DBG_parent_str, MPIR_Process.comm_world->rank, vc->pg_rank, VCState(vc), (vc->pg && vc->pg_rank >= 0) ? vc->pg->id : "?");fflush(stdout);*/    vc->ch.shm_next_reader = MPIDI_CH3I_Process.shm_reading_list;    MPIDI_CH3I_Process.shm_reading_list = vc;}#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_SHM_Add_to_writer_list#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)void MPIDI_CH3I_SHM_Add_to_writer_list(MPIDI_VC_t *vc){    MPIDI_CH3I_SHM_Remove_vc_write_references(vc);    /*printf("[%s%d]adding write vc%d.state = %s (%s)\n", MPIU_DBG_parent_str, MPIR_Process.comm_world->rank, vc->pg_rank, VCState(vc), (vc->pg && vc->pg_rank >= 0) ? vc->pg->id : "?");fflush(stdout);*/    vc->ch.shm_next_writer = MPIDI_CH3I_Process.shm_writing_list;    MPIDI_CH3I_Process.shm_writing_list = vc;}#if 0static unsigned int GetIP(char *pszIP){    unsigned int nIP;    unsigned int a,b,c,d;    if (pszIP == NULL)	return 0;    sscanf(pszIP, "%u.%u.%u.%u", &a, &b, &c, &d);    /*MPIU_DBG_PRINTF(("mask: %u.%u.%u.%u\n", a, b, c, d));*/    nIP = (d << 24) | (c << 16) | (b << 8) | a;    return nIP;}static unsigned int GetMask(char *pszMask){    int i, nBits;    unsigned int nMask = 0;    unsigned int a,b,c,d;    if (pszMask == NULL)	return 0;    if (strstr(pszMask, "."))    {	sscanf(pszMask, "%u.%u.%u.%u", &a, &b, &c, &d);	/*MPIU_DBG_PRINTF(("mask: %u.%u.%u.%u\n", a, b, c, d));*/	nMask = (d << 24) | (c << 16) | (b << 8) | a;    }    else    {	nBits = atoi(pszMask);	for (i=0; i<nBits; i++)	{	    nMask = nMask << 1;	    nMask = nMask | 0x1;	}    }    /*    unsigned int a, b, c, d;    a = ((unsigned char *)(&nMask))[0];    b = ((unsigned char *)(&nMask))[1];    c = ((unsigned char *)(&nMask))[2];    d = ((unsigned char *)(&nMask))[3];    MPIU_DBG_PRINTF(("mask: %u.%u.%u.%u\n", a, b, c, d));    */    return nMask;}static int GetHostAndPort(char *host, int *port, char *business_card){    char pszNetMask[50];    char *pEnv, *token;    unsigned int nNicNet, nNicMask;    char *temp, *pszHost, *pszIP, *pszPort;    unsigned int ip;    pEnv = getenv("MPICH_NETMASK");    if (pEnv != NULL)    {	MPIU_Strncpy(pszNetMask, pEnv, 50);	token = strtok(pszNetMask, "/");	if (token != NULL)	{	    token = strtok(NULL, "\n");	    if (token != NULL)	    {		nNicNet = GetIP(pszNetMask);		nNicMask = GetMask(token);		/* parse each line of the business card and match the ip address with the network mask */		temp = MPIU_Strdup(business_card);		token = strtok(temp, ":\r\n");		while (token)		{		    pszHost = token;		    pszIP = strtok(NULL, ":\r\n");		    pszPort = strtok(NULL, ":\r\n");		    ip = GetIP(pszIP);		    /*msg_printf("masking '%s'\n", pszIP);*/		    if ((ip & nNicMask) == nNicNet)		    {			/* the current ip address matches the requested network so return these values */			MPIU_Strncpy(host, pszIP, MAXHOSTNAMELEN); /*pszHost);*/			*port = atoi(pszPort);			MPIU_Free(temp);			return MPI_SUCCESS;		    }		    token = strtok(NULL, ":\r\n");		}		if (temp)		    MPIU_Free(temp);	    }	}    }    temp = MPIU_Strdup(business_card);    if (temp == NULL)    {	/*MPIDI_err_printf("GetHostAndPort", "MPIU_Strdup failed\n");*/	return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER, "**MPIU_Strdup", 0);    }    /* move to the host part */    token = strtok(temp, ":");    if (token == NULL)    {	MPIU_Free(temp);	/*MPIDI_err_printf("GetHostAndPort", "invalid business card\n");*/	return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER, "**business_card", "**business_card %s", business_card); /*"[ch3:mm] GetHostAndPort: Invalid business card - %s", business_card);*/    }    /*strcpy(host, token);*/    /* move to the ip part */    token = strtok(NULL, ":");    if (token == NULL)    {	MPIU_Free(temp);	/*MPIDI_err_printf("GetHostAndPort", "invalid business card\n");*/	return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER, "**business_card", "**business_card %s", business_card); /*"[ch3:mm] GetHostAndPort: Invalid business card - %s", business_card);*/    }    MPIU_Strncpy(host, token, MAXHOSTNAMELEN); /* use the ip string instead of the hostname, it's more reliable */    /* move to the port part */    token = strtok(NULL, ":");    if (token == NULL)    {	MPIU_Free(temp);	/*MPIDI_err_printf("GetHostAndPort", "invalid business card\n");*/	return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER, "**business_card", "**business_card %s", business_card); /*"[ch3:mm] GetHostAndPort: Invalid business card - %s", business_card);*/    }    *port = atoi(token);    MPIU_Free(temp);    return MPI_SUCCESS;}#endif/* This routine may be called from the common connect code */#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_Shm_connect#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)int MPIDI_CH3I_Shm_connect(MPIDI_VC_t *vc, const char *business_card, int *flag){    int mpi_errno;    char hostname[256];    char queue_name[100];    MPIDI_CH3I_BootstrapQ queue;    MPIDI_CH3I_Shmem_queue_info shm_info;    int i;#ifdef HAVE_SHARED_PROCESS_READ    char pid_str[20];#ifndef HAVE_WINDOWS_H    char filename[256];#endif#endif    /* get the host and queue from the business card */    mpi_errno = MPIU_Str_get_string_arg(business_card, MPIDI_CH3I_SHM_HOST_KEY, hostname, 256);    if (mpi_errno != MPIU_STR_SUCCESS)    {	/*printf("getstringarg(%s, %s) failed.\n", MPIDI_CH3I_SHM_HOST_KEY, business_card);fflush(stdout);*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -