コード例 #1
0
ファイル: netstats.c プロジェクト: Zsedo/ergm
void network_stats_wrapper(int *tails, int *heads, int *timings, int *time, int *lasttoggle, int *dnedges, 
			   int *dn, int *dflag,  int *bipartite,
			   int *nterms, char **funnames,
			   char **sonames, double *inputs,  double *stats)
{
  int directed_flag;
  Vertex n_nodes;
  Edge n_edges;
  Network nw[2];
  Model *m;
  Vertex bip;

/*	     Rprintf("prestart with setup\n"); */
  n_nodes = (Vertex)*dn; 
  n_edges = (Edge)*dnedges;     
  directed_flag = *dflag;
  bip = (Vertex)*bipartite;
  

  if(*lasttoggle == 0) lasttoggle = NULL;

  m=ModelInitialize(*funnames, *sonames, &inputs, *nterms);
  nw[0]=NetworkInitialize(NULL, NULL, 0,
                          n_nodes, directed_flag, bip, *timings?1:0, *timings?*time:0, *timings?lasttoggle:NULL);

  /* Compute the change statistics and copy them to stats for return
     to R.  Note that stats already has the statistics of an empty
     network, so d_??? statistics will add on to them, while s_???
     statistics will simply overwrite them. */
  SummStats(n_edges, tails, heads, nw, m, stats);
  
  ModelDestroy(m);
  NetworkDestroy(nw);
}
コード例 #2
0
ファイル: netstats.c プロジェクト: yngcan/cotergm
void network_stats_wrapper(int *tails, int *heads, int *timings, int *time, int *lasttoggle, int *dnedges, 
			   int *dn,
				int *y0tails, int *y0heads,int *y0dn,int *y0dnedges,double *y0nodalstatus,

				int *dflag,  int *bipartite,
			   int *nterms, char **funnames,
			   char **sonames, double *inputs,   double *nodalstatus, double *stats)
{
  int directed_flag;
  Vertex n_nodes;
  Edge n_edges, y0n_edges;
  Network nw[2];
  Network y0[1];
  Model *m;
  Vertex bip;

  double * negstatus;


/*	     Rprintf("prestart with setup\n"); */
  n_nodes = (Vertex)*dn; 
  Vertex y0n_nodes =  (Vertex)*y0dn;
  n_edges = (Edge)*dnedges;
  y0n_edges = (Edge)*y0dnedges;
  directed_flag = *dflag;
  bip = (Vertex)*bipartite;
  
  negstatus = (double *)malloc( n_nodes * sizeof(double));

  /*set to negative */
  for (int i=0; i<n_nodes; i++)
	  negstatus[i] = 1.0 ;

  if(*lasttoggle == 0) lasttoggle = NULL;

  m=ModelInitialize(*funnames, *sonames, &inputs, *nterms, n_nodes, nodalstatus);

  nw[0]=NetworkInitialize(NULL, NULL, 0,
                          n_nodes, directed_flag, bip, *timings?1:0, *timings?*time:0, *timings?lasttoggle:NULL, negstatus);

  y0[0]=NetworkInitialize(y0tails, y0heads, y0n_edges,
				y0n_nodes, directed_flag, bip, 0, 0, NULL, y0nodalstatus);
  /* Compute the change statistics and copy them to stats for return
     to R.  Note that stats already has the statistics of an empty
     network, so d_??? statistics will add on to them, while s_???
     statistics will simply overwrite them. */
  SummStats(n_edges, n_nodes, tails, heads, nw, m, stats, y0);
  
  ModelDestroy(m);
  NetworkDestroy(nw);
}
コード例 #3
0
ファイル: MCMC.c プロジェクト: Zsedo/ergm
void MCMCPhase12 (int *tails, int *heads, int *dnedges, 
		  int *dn, int *dflag, int *bipartite, 
		  int *nterms, char **funnames,
		  char **sonames, 
		  char **MHproposaltype, char **MHproposalpackage,
		  double *inputs, 
		  double *theta0, int *samplesize,
		  double *gain, double *meanstats, int *phase1, int *nsub,
		  double *sample, int *burnin, int *interval,  
		  int *newnetworktails, 
		  int *newnetworkheads, 
		  int *fVerbose, 
		  int *attribs, int *maxout, int *maxin, int *minout,
		  int *minin, int *condAllDegExact, int *attriblength, 
		  int *maxedges,
		  int *mtails, int *mheads, int *mdnedges)  {
  int directed_flag;
  int nphase1, nsubphases;
  Vertex n_nodes, bip;
  Edge n_edges, nmax;
  Network nw[2];
  Model *m;
  MHproposal MH;
  
  nphase1 = *phase1; 
  nsubphases = *nsub;

  n_nodes = (Vertex)*dn; 
  n_edges = (Edge)*dnedges; 
  nmax = (Edge)*maxedges; 
  bip = (Vertex)*bipartite; 
  
  GetRNGstate();  /* R function enabling uniform RNG */
  
  directed_flag = *dflag;

  m=ModelInitialize(*funnames, *sonames, &inputs, *nterms);

  /* Form the missing network */
  nw[0]=NetworkInitialize(tails, heads, n_edges,
                          n_nodes, directed_flag, bip, 0, 0, NULL);


  MH_init(&MH,
	  *MHproposaltype, *MHproposalpackage,
	  inputs,
	  *fVerbose,
	  nw, attribs, maxout, maxin, minout, minin,
	  *condAllDegExact, *attriblength);
  
  MCMCSamplePhase12 (&MH,
		     theta0, *gain, meanstats, nphase1, nsubphases, sample, *samplesize,
		     *burnin, *interval,
		     (int)*fVerbose, nw, m);

  MH_free(&MH);
  
  /* record new generated network to pass back to R */
  if(nmax>0 && newnetworktails && newnetworkheads)
    newnetworktails[0]=newnetworkheads[0]=EdgeTree2EdgeList(newnetworktails+1,newnetworkheads+1,nw,nmax-1);

  ModelDestroy(m);

  NetworkDestroy(nw);
  PutRNGstate();  /* Disable RNG before returning */
}
コード例 #4
0
ファイル: MCMC.c プロジェクト: Zsedo/ergm
/*****************
 void MCMC_wrapper

 Wrapper for a call from R.

 and don't forget that tail -> head
*****************/
void MCMC_wrapper(int *dnumnets, int *nedges,
		  int *tails, int *heads,
		  int *dn, int *dflag, int *bipartite, 
		  int *nterms, char **funnames,
		  char **sonames, 
		  char **MHproposaltype, char **MHproposalpackage,
		  double *inputs, double *theta0, int *samplesize, 
		  double *sample, int *burnin, int *interval,  
		  int *newnetworktails, 
		  int *newnetworkheads, 
		  int *fVerbose, 
		  int *attribs, int *maxout, int *maxin, int *minout,
		  int *minin, int *condAllDegExact, int *attriblength, 
		  int *maxedges,
		  int *status){
  int directed_flag;
  Vertex n_nodes, nmax, bip;
  /* Edge n_networks; */
  Network nw[1];
  Model *m;
  MHproposal MH;
  
  n_nodes = (Vertex)*dn; 
  /* n_networks = (Edge)*dnumnets;  */
  nmax = (Edge)abs(*maxedges);
  bip = (Vertex)*bipartite; 
  
  GetRNGstate();  /* R function enabling uniform RNG */
  
  directed_flag = *dflag;

  m=ModelInitialize(*funnames, *sonames, &inputs, *nterms);

  /* Form the network */
  nw[0]=NetworkInitialize(tails, heads, nedges[0], 
                          n_nodes, directed_flag, bip, 0, 0, NULL);
  
  MH_init(&MH,
	  *MHproposaltype, *MHproposalpackage,
	  inputs,
	  *fVerbose,
	  nw, attribs, maxout, maxin, minout, minin,
	  *condAllDegExact, *attriblength);

  *status = MCMCSample(&MH,
		       theta0, sample, *samplesize,
		       *burnin, *interval,
		       *fVerbose, nmax, nw, m);
  
  MH_free(&MH);
        
/* Rprintf("Back! %d %d\n",nw[0].nedges, nmax); */

  /* record new generated network to pass back to R */
  if(*status == MCMC_OK && *maxedges>0 && newnetworktails && newnetworkheads)
    newnetworktails[0]=newnetworkheads[0]=EdgeTree2EdgeList(newnetworktails+1,newnetworkheads+1,nw,nmax-1);
  
  ModelDestroy(m);
  NetworkDestroy(nw);
  PutRNGstate();  /* Disable RNG before returning */
}
コード例 #5
0
ファイル: SAN.c プロジェクト: yngcan/cotergm
void SAN_wrapper ( int *dnumnets, int *nedges,
		   int *tails, int *heads,
                   int *dn,
           		int *y0tails, int *y0heads,int *y0dn,int *y0nedges,double *y0nodalstatus,
                   int *dflag, int *bipartite,
                   int *nterms, char **funnames,
                   char **sonames, 
                   char **MHproposaltype, char **MHproposalpackage,
                   double *inputs, double *nodalstatus, double *theta0, double *tau,
		   int *samplesize, 
                   double *sample, int *burnin, int *interval,  
                   int *newnetworktails, 
                   int *newnetworkheads, 
                   int *newnodalstatus,
                   double *invcov, 
                   int *fVerbose, 
                   int *attribs, int *maxout, int *maxin, int *minout,
                   int *minin, int *condAllDegExact, int *attriblength, 
                   int *maxedges,
		   int *status){
  int directed_flag;
  Vertex n_nodes, nmax, bip;
  Network nw[1],y0[1];
  Model *m;
  MHproposal MH;


  /* please don't forget:   tail -> head   */

  
  n_nodes = (Vertex)*dn; 
  Vertex y0n_nodes =  (Vertex)*y0dn;

  nmax = (Edge)abs(*maxedges);
  bip = (Vertex)*bipartite; 
  
  GetRNGstate();  /* R function enabling uniform RNG */
  
  directed_flag = *dflag;

  m=ModelInitialize(*funnames, *sonames, &inputs, *nterms, n_nodes, nodalstatus);

  /* Form the network */
  nw[0]=NetworkInitialize(tails, heads, nedges[0],
                          n_nodes, directed_flag, bip, 0, 0, NULL, nodalstatus);
  
  y0[0]=NetworkInitialize(y0tails, y0heads, y0nedges[0],
          y0n_nodes, directed_flag, bip, 0, 0, NULL, y0nodalstatus);

  MH_init(&MH,
	  *MHproposaltype, *MHproposalpackage,
	  inputs,
	  *fVerbose,
	  nw, attribs, maxout, maxin, minout, minin,
	  *condAllDegExact, *attriblength);

  *status = SANSample (&MH,
		       theta0, invcov, tau, sample, *samplesize,
		       *burnin, *interval,
		       *fVerbose, nmax, nw, m,y0);
  
  MH_free(&MH);
        
/* Rprintf("Back! %d %d\n",nw[0].nedges, nmax); */

  /* record new generated network to pass back to R */
  if(*status == MCMC_OK && *maxedges>0 && newnetworktails && newnetworkheads)
    newnetworktails[0]=newnetworkheads[0]=EdgeTree2EdgeList(newnetworktails+1,newnetworkheads+1,nw,nmax-1);
	if(*status == MCMC_OK && *maxedges>0 && newnodalstatus){
		for (int i=0;i<n_nodes;i++)
			newnodalstatus[i] = nw->nodalstatus[i];
	}
  ModelDestroy(m);
  NetworkDestroy(nw);
  NetworkDestroy(y0);
  PutRNGstate();  /* Disable RNG before returning */
}
コード例 #6
0
ファイル: allstatistics.c プロジェクト: cran/ergm
void AllStatistics (
       int *tails, 
       int *heads,
       int *dnedges,
		   int *dn, /* Number of nodes */
       int *dflag, /* directed flag */
       int *bipartite,
       int *nterms, 
		   char **funnames, 
       char **sonames, 
       double *inputs,  
       double *covmat,
		   int *weightsvector,
       int *maxNumDyadTypes) {
  Network *nwp;

  Vertex n_nodes = (Vertex) *dn; 
  unsigned int directed_flag = *dflag;
  Vertex nodelistlength, rowmax, *nodelist1, *nodelist2;
  Vertex bip = (Vertex) *bipartite;
  Model *m;
  ModelTerm *mtp;

  /* Step 1:  Initialize empty network and initialize model */
  GetRNGstate(); /* Necessary for R random number generator */
  nwp=NetworkInitialize((Vertex*)tails, (Vertex*)heads, *dnedges,
		       n_nodes, directed_flag, bip, 0, 0, NULL);
  m=ModelInitialize(*funnames, *sonames, &inputs, *nterms);
  
  /* Step 2:  Build nodelist1 and nodelist2, which together give all of the
  dyads in the network. */
  if (BIPARTITE > 0) { /* Assuming undirected in the bipartite case */
    nodelistlength = BIPARTITE * (N_NODES-BIPARTITE);
    rowmax = BIPARTITE + 1;
  } else {
    nodelistlength = N_NODES * (N_NODES-1) / (DIRECTED? 1 : 2);
    rowmax = N_NODES;
  }
  nodelist1 = (Vertex *) R_alloc(nodelistlength, sizeof(int));
  nodelist2 = (Vertex *) R_alloc(nodelistlength, sizeof(int));
  int count = 0;
  for(int i=1; i < rowmax; i++) {
    for(int j = MAX(i,BIPARTITE)+1; j <= N_NODES; j++) {
      for(int d=0; d <= DIRECTED; d++) { /*trivial loop if undirected*/
        nodelist1[count] = d==1? j : i;
        nodelist2[count] = d==1? i : j;
        count++;
      }
    }
  }

  /* Step 3:  Initialize values of mtp->dstats so they point to the correct
  spots in the newRow vector.  These values will never change. */
  double *changeStats     = (double *) R_alloc(m->n_stats,sizeof(double));
  double *cumulativeStats = (double *) R_alloc(m->n_stats,sizeof(double));
  for (int i=0; i < m->n_stats; i++) cumulativeStats[i]=0.0;

  unsigned int totalStats = 0;
  for (mtp=m->termarray; mtp < m->termarray + m->n_terms; mtp++){
    mtp->dstats = changeStats + totalStats;
    /* Update mtp->dstats pointer to skip atail by mtp->nstats */
    totalStats += mtp->nstats; 
  }
  if (totalStats != m->n_stats) {
    Rprintf("I thought totalStats=%d and m->nstats=%d should be the same.\n", 
    totalStats, m->n_stats);
  }

  /* Step 4:  Begin recursion */
  RecurseOffOn(nodelist1, nodelist2, nodelistlength, 0, changeStats, 
	       cumulativeStats, covmat, (unsigned int*) weightsvector, *maxNumDyadTypes, nwp, m);

  /* Step 5:  Deallocate memory and return */
  ModelDestroy(m);
  NetworkDestroy(nwp);
  PutRNGstate(); /* Must be called after GetRNGstate before returning to R */
}
コード例 #7
0
ファイル: netinfo.c プロジェクト: UVP-Tools/UVP-Tools
/*****************************************************************************
Function   : GetIpv6Info
Description: get Ipv4/6 info
Input       : None
Output     : None
Return     : 
*****************************************************************************/
int GetIpv6Info()
{
    struct ifconf ifconfigure;
    struct ifreq *ifreqIdx;
    struct ifreq *ifreq;
    char *path="/proc/net/dev";
    FILE *file;
    char *line = NULL;
    size_t linelen = 0;
    char namebuf[16] = {0};
    char buf[4096] = {0};
    int uNICCount = 0;
    int num = 0;
    int i = 0;
    int novifnameFlag = 0;
    int lastcount = 0;
    int getipv6flag = 0;
    int vifnameLen = 0;
    int skt;

    
    ifconfigure.ifc_len = 4096;
    ifconfigure.ifc_buf = buf;
    
    skt = openNetSocket(); 
    if (ERROR == skt)
    {
        DEBUG_LOG("Failed to openNetSocket.");
        return ERROR;
    }
    memset_s(&gtNicIpv6Info, sizeof(gtNicIpv6Info), 0, sizeof(gtNicIpv6Info)); 


    /*ioctl interface: interface has ipv4 and its status is up*/
    if(!ioctl(skt, SIOCGIFCONF, (char *) &ifconfigure))
    {
        ifreq = (struct ifreq *)buf;
        uNICCount = ifconfigure.ifc_len / (int)sizeof(struct ifreq);
        for(i=0; i<uNICCount; i++)
        {
            int nicRepeatFlag = 0;
            int j = 0;
            ifreqIdx = ifreq + i;
            memset_s(namebuf, 16, 0, 16);
            (void)snprintf_s(namebuf, sizeof(namebuf), sizeof(namebuf), "%s", ifreqIdx->ifr_name);

            for(j = 0; num > 0 && j < num; j++)
            {
                /*do not modify strcmp -> strncmp*/
                if(0 == strcmp(namebuf, gtNicIpv6Info.info[j].ifname))
                {
                    nicRepeatFlag = 1;
                    break;
                }
            }

            vifnameLen = strlen(namebuf);
            if(nicRepeatFlag || 0 == strncmp(namebuf,"lo",vifnameLen) || 0 == strncmp(namebuf,"sit0",vifnameLen))
            {
                continue;
            }

            /*get ipv4 info */
            if(UPFLAG == GetVifFlag(skt, namebuf))
            {
                getipv6flag = GetIpv4VifIp(skt,num,namebuf);
                if(ERROR != getipv6flag)
                {
                    num = getipv6flag;
                }

                if(num >= IPV6_NIC_MAX)
                    break;
            }

            /*get ipv6 info, /proc/net/if_inet6: the interface status is up */
            getipv6flag = GetIpv6VifIp(skt,num,namebuf);
            if(ERROR != getipv6flag)
            {
               num = getipv6flag;
            }

            if(num >= IPV6_NIC_MAX)
                  break;

        }
    }
    
    /*patch interface by query /proc/net/dev*/ 
   if(NULL == (file = opendevfile(path)))
    {
        NetworkDestroy(skt);
        DEBUG_LOG("Failed to open /proc/net/dev.");
        return ERROR;
    }
    
   if(getline(&line, &linelen, file) == -1 /* eat line */
        || getline(&line, &linelen, file) == -1) {}
   while (getline(&line, &linelen, file) != -1)
    {
        novifnameFlag = 0;
        char *vifname;
        (void)GetVifName(&vifname, line);
        vifnameLen = strlen(vifname);
        if(0 == strncmp(vifname,"lo",vifnameLen) || 0== strncmp(vifname,"sit0",vifnameLen))
        {
           continue;
        }
        
        for(i=0;i<num;i++)
        {
           /*do not modify strcmp -> strncmp*/
           if(0 == strcmp(vifname,gtNicIpv6Info.info[i].ifname))
           {
                   /*has exist vifname*/
                novifnameFlag = 1;
           }
        }
        if(num >= IPV6_NIC_MAX)
                  break;
          /*new vifname*/
        if(novifnameFlag == 0)
        {
            /*if interface status is down, then xenstore info only has mac and its status*/
            if(DOWNFLAG == GetVifFlag(skt,vifname))
            {
                num++;
                GetIpv6VifMac(skt, vifname);
                GetIpv6NetFlag(skt,vifname);
                gtNicIpv6Info.count=num;

            }
            else
            {
                /*if interface status is up and no ip, then xenstore info has mac,staus,ip(none),gw,flux*/
                /*if interface status is up and has ipv6 info,then xenstore info has mac,staus,ip6,gw,flux*/
                lastcount = gtNicIpv6Info.count;
                getipv6flag = GetIpv6VifIp(skt,num,vifname);
                if(ERROR != getipv6flag)
                {
                    num = getipv6flag;
                }
                if(num == lastcount)
                {
                    num++;
                    GetIpv6VifMac(skt, vifname);
                    gtNicIpv6Info.info[gtNicIpv6Info.count].gateway[0] = '\0';
                    (void)strncpy_s(gtNicIpv6Info.info[gtNicIpv6Info.count].ipaddr,
                        IPV6_ADDR_LEN, 
                        "none", 
                        strlen("none"));
                    gtNicIpv6Info.info[gtNicIpv6Info.count].ipaddr[sizeof(gtNicIpv6Info.info[gtNicIpv6Info.count].ipaddr)-1]='\0';
                    GetIpv6Flux(skt,vifname);

                    if(g_exinfo_flag_value & EXINFO_FLAG_GATEWAY)
                    {
                        GetIpv4VifGateway(skt,vifname);
                    }
                    else
                    {
                        (void)strncpy_s(gtNicIpv6Info.info[gtNicIpv6Info.count].gateway, 
                            IPV6_ADDR_LEN, 
                            "0", 
                            strlen("0"));
                    }
                    GetIpv6NetFlag(skt,vifname);
                    gtNicIpv6Info.count =num;
                }
                else
                {
                    gtNicIpv6Info.count =num;
                }
            }

        }
    }
   fclose(file);
   free(line);
   NetworkDestroy(skt);
   return gtNicIpv6Info.count; 
}