Example #1
0
/*
 * ep_legal()
 *	Returns, whether in the specified board the B at the specified position
 *	may legally beat e.p. The third position is the Bs king.
 *	We must not be in check, currently.
 *	Note:	Here is checked the possible double uncovering through both B.
 *		Other uncoverings through the beaten B are impossible,
 *		and simple uncoverings through the beating B have to be checked
 *		outside this function.
 */
Eximpl Bool
ep_legal(
    const Board*	bp,
    Position		pos,
    Position		tpos,
    Position		kpos)
{
    int			delta;
    int			xpos;
    int			j;
    int			enemy;

    if( bp->b_ep != (tpos - bau_mov[bp->b_tomove]) )
        return FALSE;
    /*
     * uncovering by beat bp->b_ep is possible
     * only horizontally through both B:
     */
    if( LIN(kpos) != LIN(pos) )
        return TRUE;
    if( kpos < pos ) {
        delta = MK_DELTA( 1,0);
    } else {
        delta = MK_DELTA(-1,0);
    }
    enemy = opp_colour(bp->b_tomove);
    xpos = kpos;
    j = 0;
    for(;;) {
        xpos += delta;
        if( bp->b_f[xpos].f_c == empty )
            continue;
        if( bp->b_f[xpos].f_c == border )
            break;
        if( xpos == pos || xpos == bp->b_ep ) {
            ++j;
            continue;
        }
        if( j == 2 && bp->b_f[xpos].f_c == enemy ) {
            switch( bp->b_f[xpos].f_f ) {
            case turm:
            case dame:
                ++j;
                break;
            }
        }
        break;
    }
    return j < 3;
}
Example #2
0
File: hex.c Project: mrahn/hex
static void show (PPosition_type pos)
{
  printf ("%s%s\n", show_player[pos->player], show_player[pos->winner]);

  for (int x = 0; x <= 2 * SIZE; ++x)
  {
    for (int y = -2 * SIZE; y <= 2 * SIZE; ++y)
    {
      int const qx = (2 * x + y) / 4;
      int const rx = (2 * x + y) % 4;

      int const qy = (2 * x - y) / 4;
      int const ry = (2 * x - y) % 4;

      if (  rx == 0
         && ry == 0
         && qx >= 0 && qx <= SIZE
         && qy >= 0 && qy <= SIZE
         )
      {
        printf ("%s", show_player[pos->taken[LIN (qx, qy)]]);
      }
      else
      {
        printf (" ");
      }
    }
    printf ("\n");
  }
}
int main(int argc, char* argv[]) {
    int i, j;
    int n = (SIZE);
    double A[(SIZE) * (SIZE)] = {0};
    double *L = NULL;
    FILE *fp = NULL;
    char filename[150];

    sprintf(filename, "%s/matrices/%d", PATH, SIZE);
    fp = fopen(filename, "r");
    if (fp == NULL) {
        return 1;
    }

    for (i = 0; i < n * n; ++i) {
        fscanf(fp, "%lf", A + i);  
    }

    start_clock();
    start_papi();
    L = cholesky(A, n);
    stop_papi();
    stop_clock();

    double checksum = 0.0;
    for (i = 0; i < n; ++i) {
        for (j = 0; j < n; ++j) {
            if (j <= i) {
                checksum += i + j + L[LIN(i, j)];
            } else {
                checksum += i + j;
            }
        }
    }

    printf("Size: %d\n", n);
    printf("Checksum: %lf\n", checksum);
    return 0;
}
Example #4
0
File: hex.c Project: mrahn/hex
static Word_t encode (PPosition_type pos)
{
  Word_t Index = 0;

  for (int i = 0; i < LEN * LEN; ++i)
  {
    Index <<= 2;
    Index += pos->taken[i];
  }

  Word_t Mirror = 0;

  for (int i = LEN * LEN - 1; i >= 0; --i)
  {
    Mirror <<= 2;
    Mirror += pos->taken[i];

    assert (LEN * LEN - 1 - i == LIN (SIZE - X(i), SIZE - Y(i)));
  }

  return MIN (Index, Mirror);
}
Example #5
0
void RCScaleChannels(void)
{
#define LIN(CHANNEL,RCTYPE) ((CHANNEL##_SLOPE*RCTYPE##_RAW_##CHANNEL)-CHANNEL##_SHIFT)
    switch (rcType) {
        case RC_WK2402:
            RC_PITCH        = LIN(PITCH,WK2402);
            RC_ROLL        = LIN(ROLL,WK2402);
            RC_THROTTLE     = LIN(THROTTLE,WK2402);
            RC_YAW           = LIN(YAW,WK2402);
            RC_PITCH_TRIM    = LIN(PITCH_TRIM,WK2402);
            RC_ROLL_TRIM    = LIN(ROLL_TRIM,WK2402);
            RC_THROTTLE_TRIM = LIN(THROTTLE_TRIM,WK2402);
            RC_YAW_TRIM       = LIN(YAW_TRIM,WK2402);
            break;
        case RC_WK2401:
        default:
            RC_PITCH        = LIN(PITCH,WK2401);
            RC_ROLL        = LIN(ROLL,WK2401);
            RC_THROTTLE     = LIN(THROTTLE,WK2401);
            RC_YAW           = LIN(YAW,WK2401);
            RC_PITCH_TRIM    = LIN(PITCH_TRIM,WK2401);
            RC_ROLL_TRIM    = LIN(ROLL_TRIM,WK2401);
            RC_THROTTLE_TRIM = LIN(THROTTLE_TRIM,WK2401);
            RC_YAW_TRIM       = LIN(YAW_TRIM,WK2401);
            break;
    }
#undef LIN

    if (RC_PITCH > 1.0)
        RC_PITCH = 1.0;
    else if (RC_PITCH < -1.0)
        RC_PITCH = -1.0;

    if (RC_ROLL > 1.0)
        RC_ROLL = 1.0;
    else if (RC_ROLL < -1.0)
        RC_ROLL = -1.0;

    if (RC_THROTTLE > 1.0)
        RC_THROTTLE = 1.0;
    else if (RC_THROTTLE < 0.0)
        RC_THROTTLE = 0.0;

    if (RC_YAW > 1.0)
        RC_YAW = 1.0;
    else if (RC_YAW < -1.0)
        RC_YAW = -1.0;

    if (RC_PITCH_TRIM > 1.0)
        RC_PITCH_TRIM = 1.0;
    else if (RC_PITCH_TRIM < -1.0)
        RC_PITCH_TRIM = -1.0;

    if (RC_ROLL_TRIM > 1.0)
        RC_ROLL_TRIM = 1.0;
    else if (RC_ROLL_TRIM < -1.0)
        RC_ROLL_TRIM = -1.0;

    if (RC_THROTTLE_TRIM > 1.0)
        RC_THROTTLE_TRIM = 1.0;
    else if (RC_THROTTLE_TRIM < 0.0)
        RC_THROTTLE_TRIM = 0.0;

    if (RC_YAW_TRIM > 1.0)
        RC_YAW_TRIM = 1.0;
    else if (RC_YAW_TRIM < -1.0)
        RC_YAW_TRIM = -1.0;
}
Example #6
0
int main(int argc, char **argv){

  int               iter, r;    /* dummies                                        */
  int               lsize;      /* logarithmic linear size of grid                */
  int               lsize2;     /* logarithmic size of grid                       */
  int               size;       /* linear size of grid                            */
  s64Int            size2;      /* matrix order (=total # points in grid)         */
  int               radius,     /* stencil parameters                             */
                    stencil_size; 
  s64Int            row, col, first, last; /* dummies                             */
  s64Int            i, j;       /* dummies                                        */
  int               iterations; /* number of times the multiplication is done     */
  s64Int            elm;        /* sequence number of matrix nonzero              */
  s64Int            nent;       /* number of nonzero entries                      */
  double            sparsity;   /* fraction of non-zeroes in matrix               */
  double            sparse_time,/* timing parameters                              */
                    avgtime = 0.0, 
                    maxtime = 0.0, 
                    mintime = 366.0*24.0*3600.0; /* set the minimum time to 
                             a large value; one leap year should be enough        */
  double * RESTRICT matrix;     /* sparse matrix entries                          */
  double * RESTRICT vector;     /* vector multiplying the sparse matrix           */
  double * RESTRICT result;     /* computed matrix-vector product                 */
  double            temp;       /* temporary scalar storing reduction data        */
  double            vector_sum; /* checksum of result                             */
  double            reference_sum; /* checksum of "rhs"                           */
  double            epsilon = 1.e-8; /* error tolerance                           */
  s64Int * RESTRICT colIndex;   /* column indices of sparse matrix entries        */
  int               nthread_input,  /* thread parameters                          */
                    nthread;   
  int               num_error=0; /* flag that signals that requested and 
                                   obtained numbers of threads are the same       */
  size_t            vector_space, /* variables used to hold malloc sizes          */
                    matrix_space,
                    index_space;

  if (argc != 5) {
    printf("Usage: %s <# threads> <# iterations> <2log grid size> <stencil radius>\n",*argv);
    exit(EXIT_FAILURE);
  }

  /* Take number of threads to request from command line                          */
  nthread_input = atoi(*++argv); 

  if ((nthread_input < 1) || (nthread_input > MAX_THREADS)) {
    printf("ERROR: Invalid number of threads: %d\n", nthread_input);
    exit(EXIT_FAILURE);
  }

  omp_set_num_threads(nthread_input);
 
  iterations = atoi(*++argv);
  if (iterations < 1){
    printf("ERROR: Iterations must be positive : %d \n", iterations);
    exit(EXIT_FAILURE);
  }

  lsize = atoi(*++argv);
  lsize2 = 2*lsize;
  size = 1<<lsize;
  if (lsize <0) {
    printf("ERROR: Log of grid size must be greater than or equal to zero: %d\n", 
           (int) lsize);
    exit(EXIT_FAILURE);
  }
  /* compute number of points in the grid                                         */
  size2 = size*size;

  radius = atoi(*++argv);
  if (radius <0) {
    printf("ERROR: Stencil radius must be non-negative: %d\n", (int) size);
    exit(EXIT_FAILURE);
  }

  /* emit error if (periodic) stencil overlaps with itself                        */
  if (size <2*radius+1) {
    printf("ERROR: Grid extent %d smaller than stencil diameter 2*%d+1= %d\n",
           size, radius, radius*2+1);
    exit(EXIT_FAILURE);
  }
 
  /* compute total size of star stencil in 2D                                     */
  stencil_size = 4*radius+1;
  /* sparsity follows from number of non-zeroes per row                           */
  sparsity = (double)(4*radius+1)/(double)size2;

  /* compute total number of non-zeroes                                           */
  nent = size2*stencil_size;

  matrix_space = nent*sizeof(double);
  if (matrix_space/sizeof(double) != nent) {
    printf("ERROR: Cannot represent space for matrix: %ld\n", matrix_space);
    exit(EXIT_FAILURE);
  } 

  matrix = (double *) malloc(matrix_space);
  if (!matrix) {
    printf("ERROR: Could not allocate space for sparse matrix: "FSTR64U"\n", nent);
    exit(EXIT_FAILURE);
  } 

  vector_space = 2*size2*sizeof(double);
  if (vector_space/sizeof(double) != 2*size2) {
    printf("ERROR: Cannot represent space for vectors: %ld\n", vector_space);
    exit(EXIT_FAILURE);
  } 

  vector = (double *) malloc(vector_space);
  if (!vector) {
    printf("ERROR: Could not allocate space for vectors: %d\n", (int)(2*size2));
    exit(EXIT_FAILURE);
  }
  result = vector + size2;

  index_space = nent*sizeof(s64Int);
  if (index_space/sizeof(s64Int) != nent) {
    printf("ERROR: Cannot represent space for column indices: %ld\n", index_space);
    exit(EXIT_FAILURE);
  } 
  colIndex = (s64Int *) malloc(index_space);
  if (!colIndex) {
    printf("ERROR: Could not allocate space for column indices: "FSTR64U"\n",
           nent*sizeof(s64Int));
    exit(EXIT_FAILURE);
  } 

  #pragma omp parallel private (row, col, elm, first, last, iter)
  {

  #pragma omp master 
  {
  nthread = omp_get_num_threads();

  printf("OpenMP Sparse matrix-vector multiplication\n");
  if (nthread != nthread_input) {
    num_error = 1;
    printf("ERROR: number of requested threads %d does not equal ",
           nthread_input);
    printf("number of spawned threads %d\n", nthread);
  } 
  else {
    printf("Number of threads     = %16d\n",nthread_input);
    printf("Matrix order          = "FSTR64U"\n", size2);
    printf("Stencil diameter      = %16d\n", 2*radius+1);
    printf("Sparsity              = %16.10lf\n", sparsity);
#ifdef SCRAMBLE
    printf("Using scrambled indexing\n");
#else
    printf("Using canonical indexing\n");
#endif
    printf("Number of iterations  = %16d\n", iterations);
  }
  }
  bail_out(num_error);

  /* initialize the input and result vectors                                      */
  #pragma omp for
  for (row=0; row<size2; row++) result[row] = vector[row] = 0.0;

  /* fill matrix with nonzeroes corresponding to difference stencil. We use the 
     scrambling for reordering the points in the grid.                            */

  #pragma omp for private (i,j,r)
  for (row=0; row<size2; row++) {
    j = row/size; i=row%size;
    elm = row*stencil_size;
    colIndex[elm] = REVERSE(LIN(i,j),lsize2);
    for (r=1; r<=radius; r++, elm+=4) {
      colIndex[elm+1] = REVERSE(LIN((i+r)%size,j),lsize2);
      colIndex[elm+2] = REVERSE(LIN((i-r+size)%size,j),lsize2);
      colIndex[elm+3] = REVERSE(LIN(i,(j+r)%size),lsize2);
      colIndex[elm+4] = REVERSE(LIN(i,(j-r+size)%size),lsize2);
    }
    // sort colIndex to make sure the compressed row accesses
    // vector elements in increasing order
    qsort(&(colIndex[row*stencil_size]), stencil_size, sizeof(s64Int), compare);
    for (elm=row*stencil_size; elm<(row+1)*stencil_size; elm++)
      matrix[elm] = 1.0/(double)(colIndex[elm]+1);
  }

  for (iter=0; iter<iterations; iter++) {

    #pragma omp barrier
    #pragma omp master
    {   
    sparse_time = wtime();
    }

    /* fill vector                                                                */
    #pragma omp for 
    for (row=0; row<size2; row++) vector[row] += (double) (row+1);

    /* do the actual matrix-vector multiplication                                 */
    #pragma omp for
    for (row=0; row<size2; row++) {
      temp = 0.0;
      first = stencil_size*row; last = first+stencil_size-1;
      #pragma simd reduction(+:temp) 
      for (col=first; col<=last; col++) {
        temp += matrix[col]*vector[colIndex[col]];
      }
      result[row] += temp;
    }

    #pragma omp master
    {
    sparse_time = wtime() - sparse_time;
    if (iter>0 || iterations==1) { /* skip the first iteration                    */
      avgtime = avgtime + sparse_time;
      mintime = MIN(mintime, sparse_time);
      maxtime = MAX(maxtime, sparse_time);
    }
    }

  }

  } /* end of parallel region                                                     */

  /* verification test                                                            */
  reference_sum = 0.5 * (double) nent * (double) iterations * 
                        (double) (iterations +1);

  vector_sum = 0.0;
  for (row=0; row<size2; row++) vector_sum += result[row];
  if (ABS(vector_sum-reference_sum) > epsilon) {
    printf("ERROR: Vector sum = %lf, Reference vector sum = %lf\n",
           vector_sum, reference_sum);
    exit(EXIT_FAILURE);
  }
  else {
    printf("Solution validates\n");
#ifdef VERBOSE
    printf("Reference sum = %lf, vector sum = %lf\n", 
           reference_sum, vector_sum);
#endif
  }

  avgtime = avgtime/(double)(MAX(iterations-1,1));
  printf("Rate (MFlops/s): %lf,  Avg time (s): %lf,  Min time (s): %lf",
         1.0E-06 * (2.0*nent)/mintime, avgtime, mintime);
  printf(", Max time (s): %lf\n", maxtime);

  exit(EXIT_SUCCESS);
}
Example #7
0
static bool load_conn(struct ub_ctx *dnsctx,
		     struct starter_conn *conn,
		     struct config_parsed *cfgp,
		     struct section_list *sl,
		     bool alsoprocessing,
		     bool defaultconn,
		     bool resolvip,
		     err_t *perr)
{
	bool err = FALSE;

	err |= load_conn_basic(conn, sl,
			       defaultconn ? k_default : k_set, perr);

	move_comment_list(&conn->comments, &sl->comments);

	if (err)
		return err;

	if (conn->strings[KSF_ALSO] != NULL &&
	    !alsoprocessing) {
		starter_log(LOG_LEVEL_INFO,
			    "also= is not valid in section '%s'",
			    sl->name);
		return TRUE;	/* error */
	}

	/* now, process the also's */
	if (conn->alsos)
		FREE_LIST(conn->alsos);
	conn->alsos = new_list(conn->strings[KSF_ALSO]);

	if (alsoprocessing && conn->alsos != NULL) {
		struct section_list *sl1;
		/* note: for the duration of this loop body
		 * conn->alsos is migrated to local variable alsos.
		 */
		char **alsos = conn->alsos;
		int alsosize;
		int alsoplace;

		conn->alsos = NULL;

		/* reset all of the "beenhere" flags */
		for (sl1 = cfgp->sections.tqh_first; sl1 != NULL;
		     sl1 = sl1->link.tqe_next)
			sl1->beenhere = FALSE;
		sl->beenhere = TRUE;

		/* count them */
		for (alsosize = 0; alsos[alsosize] != NULL; alsosize++)
			;

		alsoplace = 0;
		while (alsoplace < alsosize && alsos[alsoplace] != NULL &&
		       alsoplace < ALSO_LIMIT) {
			/*
			 * for each also= listed, go find this section's keyword list, and
			 * load it as well. This may extend the also= list (and the end),
			 * which we handle by zeroing the also list, and adding to it after
			 * checking for duplicates.
			 */
			for (sl1 = cfgp->sections.tqh_first;
			     sl1 != NULL &&
			     !streq(alsos[alsoplace], sl1->name);
			     sl1 = sl1->link.tqe_next)
				;

			starter_log(LOG_LEVEL_DEBUG,
				    "\twhile loading conn '%s' also including '%s'",
				    conn->name, alsos[alsoplace]);

			/*
			 * if we found something that matches by name, and we haven't be
			 * there, then process it.
			 */
			if (sl1 && !sl1->beenhere) {
				conn->strings_set[KSF_ALSO] = FALSE;
				pfreeany(conn->strings[KSF_ALSO]);
				conn->strings[KSF_ALSO] = NULL;
				sl1->beenhere = TRUE;

				/* translate things, but do not replace earlier settings!*/
				err |= translate_conn(conn, sl1, k_set, perr);

				if (conn->strings[KSF_ALSO] != NULL) {
					/* now, check out the KSF_ALSO, and extend list if we need to */
					char **newalsos = new_list(
						conn->strings[KSF_ALSO]);

					if (newalsos != NULL) {
						char **ra;
						int newalsoplace;

						/* count them */
						for (newalsoplace = 0;
						     newalsos[newalsoplace] !=
						     NULL;
						     newalsoplace++)
							;

						/* extend conn->alss */
						ra = alloc_bytes((alsosize +
							newalsoplace + 1) *
							sizeof(char *),
							"conn->alsos");
						memcpy(ra, alsos, alsosize * sizeof(char *));
						pfree(alsos);
						alsos = ra;
						for (newalsoplace = 0;
						     newalsos[newalsoplace] !=
						     NULL;
						     newalsoplace++) {
							assert(conn != NULL);
							assert(conn->name !=
								NULL);
							starter_log(
								LOG_LEVEL_DEBUG,
								"\twhile processing section '%s' added also=%s",
								sl1->name,
								newalsos[newalsoplace]);

							alsos[alsosize++] =
								clone_str(newalsos[newalsoplace],
									"alsos");
						}
						alsos[alsosize] = NULL;
					}

					FREE_LIST(newalsos);
				}
			}
			alsoplace++;
		}

		/* migrate alsos back to conn->alsos */
		conn->alsos = alsos;

		if (alsoplace >= ALSO_LIMIT) {
			starter_log(LOG_LEVEL_INFO,
				    "while loading conn '%s', too many also= used at section %s. Limit is %d",
				    conn->name,
				    alsos[alsoplace],
				    ALSO_LIMIT);
			return TRUE;	/* error */
		}
	}

#ifdef PARSER_TYPE_DEBUG
	/* translate strings/numbers into conn items */
	starter_log(LOG_LEVEL_DEBUG,
		    "#checking options_set[KBF_TYPE,%d]=%d %d",
		    KBF_TYPE,
		    conn->options_set[KBF_TYPE], conn->options[KBF_TYPE]);
#endif

	if (conn->options_set[KBF_TYPE]) {
		switch ((enum keyword_satype)conn->options[KBF_TYPE]) {
		case KS_TUNNEL:
			conn->policy |= POLICY_TUNNEL;
			conn->policy &= ~POLICY_SHUNT_MASK;
			break;

		case KS_TRANSPORT:
			conn->policy &= ~POLICY_TUNNEL;
			conn->policy &= ~POLICY_SHUNT_MASK;
			break;

		case KS_PASSTHROUGH:
			conn->policy &=
				~(POLICY_ENCRYPT | POLICY_AUTHENTICATE |
				  POLICY_TUNNEL | POLICY_RSASIG);
			conn->policy &= ~POLICY_SHUNT_MASK;
			conn->policy |= POLICY_SHUNT_PASS;
			break;

		case KS_DROP:
			conn->policy &=
				~(POLICY_ENCRYPT | POLICY_AUTHENTICATE |
				  POLICY_TUNNEL | POLICY_RSASIG);
			conn->policy &= ~POLICY_SHUNT_MASK;
			conn->policy |= POLICY_SHUNT_DROP;
			break;

		case KS_REJECT:
			conn->policy &=
				~(POLICY_ENCRYPT | POLICY_AUTHENTICATE |
				  POLICY_TUNNEL | POLICY_RSASIG);
			conn->policy &= ~POLICY_SHUNT_MASK;
			conn->policy |= POLICY_SHUNT_REJECT;
			break;
		}
	}

	if (conn->options_set[KBF_FAILURESHUNT]) {
		conn->policy &= ~POLICY_FAIL_MASK;
		switch(conn->options[KBF_FAILURESHUNT]) {
		case KFS_FAIL_NONE:
			conn->policy |= POLICY_FAIL_NONE;
			break;
		case KFS_FAIL_PASS:
			conn->policy |= POLICY_FAIL_PASS;
			break;
		case KFS_FAIL_DROP:
			conn->policy |= POLICY_FAIL_DROP;
			break;
		case KFS_FAIL_REJECT:
			conn->policy |= POLICY_FAIL_REJECT;
			break;
		}
	}

	if (conn->options_set[KBF_NEGOTIATIONSHUNT]) {
		switch(conn->options[KBF_NEGOTIATIONSHUNT]) {
		case KNS_FAIL_PASS:
			conn->policy |= POLICY_NEGO_PASS;
			break;
		case KNS_FAIL_DROP:
			conn->policy &= ~POLICY_NEGO_PASS;
			break;
		}
	}

	KW_POLICY_FLAG(KBF_COMPRESS, POLICY_COMPRESS);
	KW_POLICY_FLAG(KBF_PFS,  POLICY_PFS);

	/* reset authby flags */
	if (conn->options_set[KBF_AUTHBY]) {
		conn->policy &= ~(POLICY_ID_AUTH_MASK);

#ifdef FIPS_CHECK
		if (libreswan_fipsmode()) {
			if (LIN(POLICY_PSK, conn->options[KBF_AUTHBY])) {
				starter_log(LOG_LEVEL_INFO,
					    "while loading conn '%s', PSK not allowed in FIPS mode with NSS",
					    conn->name);
				return TRUE;	/* error */
			}
		}
#endif

		conn->policy |= conn->options[KBF_AUTHBY];

#ifdef STARTER_POLICY_DEBUG
		starter_log(LOG_LEVEL_DEBUG,
			    "%s: setting conn->policy=%08x (%08x)",
			    conn->name,
			    (unsigned int)conn->policy,
			    conn->options[KBF_AUTHBY]);
#endif
	}

	KW_POLICY_NEGATIVE_FLAG(KBF_IKEPAD, POLICY_NO_IKEPAD);

	KW_POLICY_NEGATIVE_FLAG(KBF_REKEY, POLICY_DONT_REKEY);

	KW_POLICY_FLAG(KBF_AGGRMODE, POLICY_AGGRESSIVE);

	KW_POLICY_FLAG(KBF_MODECONFIGPULL, POLICY_MODECFG_PULL);

	KW_POLICY_FLAG(KBF_OVERLAPIP, POLICY_OVERLAPIP);

	KW_POLICY_FLAG(KBF_IKEv2_ALLOW_NARROWING,
		       POLICY_IKEV2_ALLOW_NARROWING);

	KW_POLICY_FLAG(KBF_IKEv2_PAM_AUTHORIZE,
		       POLICY_IKEV2_PAM_AUTHORIZE);

	if (conn->strings_set[KSF_ESP])
		conn->esp = clone_str(conn->strings[KSF_ESP],"KSF_ESP");

#ifdef HAVE_LABELED_IPSEC
	if (conn->strings_set[KSF_POLICY_LABEL])
		conn->policy_label = clone_str(conn->strings[KSF_POLICY_LABEL],"KSF_POLICY_LABEL");
	if (conn->policy_label != NULL)
		starter_log(LOG_LEVEL_DEBUG, "connection's  policy label: %s",
				conn->policy_label);
#endif

	if (conn->strings_set[KSF_IKE])
		conn->ike = clone_str(conn->strings[KSF_IKE],"KSF_IKE");

	if (conn->strings_set[KSF_MODECFGDNS1]) {
		conn->modecfg_dns1 = clone_str(conn->strings[KSF_MODECFGDNS1],"KSF_MODECFGDNS1");
	}
	if (conn->strings_set[KSF_MODECFGDNS2]) {
		conn->modecfg_dns2 = clone_str(conn->strings[KSF_MODECFGDNS2], "KSF_MODECFGDNS2");
	}
	if (conn->strings_set[KSF_MODECFGDOMAIN]) {
		conn->modecfg_domain = clone_str(conn->strings[KSF_MODECFGDOMAIN],"KSF_MODECFGDOMAIN");
	}
	if (conn->strings_set[KSF_MODECFGBANNER]) {
		conn->modecfg_banner = clone_str(conn->strings[KSF_MODECFGBANNER],"KSF_MODECFGBANNER");
	}

	if (conn->strings_set[KSF_CONNALIAS])
		conn->connalias = clone_str(conn->strings[KSF_CONNALIAS],"KSF_CONNALIAS");

	if (conn->options_set[KBF_PHASE2]) {
		conn->policy &= ~(POLICY_AUTHENTICATE | POLICY_ENCRYPT);
		conn->policy |= conn->options[KBF_PHASE2];
	}

	if (conn->options_set[KBF_IKEv2]) {
		lset_t policy = LEMPTY;

		switch (conn->options[KBF_IKEv2]) {
		case fo_never:
			policy = POLICY_IKEV1_ALLOW;
			break;

		case fo_permit:
			/* this is the default for now */
			policy = POLICY_IKEV1_ALLOW | POLICY_IKEV2_ALLOW;
			break;

		case fo_propose:
			policy = POLICY_IKEV1_ALLOW | POLICY_IKEV2_ALLOW | POLICY_IKEV2_PROPOSE;
			break;

		case fo_insist:
			policy =                      POLICY_IKEV2_ALLOW | POLICY_IKEV2_PROPOSE;
			break;
		}
		conn->policy = (conn->policy & ~POLICY_IKEV2_MASK) | policy;
	}

	if (conn->options_set[KBF_IKE_FRAG]) {
		switch (conn->options[KBF_IKE_FRAG]) {
		case ynf_no:
			conn->policy &= ~POLICY_IKE_FRAG_ALLOW;
			conn->policy &= ~POLICY_IKE_FRAG_FORCE;
			break;

		case ynf_yes:
			/* this is the default */
			conn->policy |= POLICY_IKE_FRAG_ALLOW;
			break;

		case ynf_force:
			conn->policy |= POLICY_IKE_FRAG_ALLOW |
					POLICY_IKE_FRAG_FORCE;
			break;
		}
	}

	if (conn->options_set[KBF_SAREFTRACK]) {
		switch (conn->options[KBF_SAREFTRACK]) {
		case sat_yes:
			/* this is the default */
			conn->policy |= POLICY_SAREF_TRACK;
			break;

		case sat_conntrack:
			conn->policy |= POLICY_SAREF_TRACK |
					POLICY_SAREF_TRACK_CONNTRACK;
			break;

		case sat_no:
			conn->policy &= ~POLICY_SAREF_TRACK;
			conn->policy &= ~POLICY_SAREF_TRACK_CONNTRACK;
			break;
		}
	}

	err |= validate_end(dnsctx, conn, &conn->left,  "left",  resolvip, perr);
	err |= validate_end(dnsctx, conn, &conn->right, "right", resolvip, perr);
	/*
	 * TODO:
	 * verify both ends are using the same inet family, if one end
	 * is "%any" or "%defaultroute", then perhaps adjust it.
	 * ensource this for left,leftnexthop,right,rightnexthop
	 * Ideally, phase out connaddrfamily= which now wrongly assumes
	 * left,leftnextop,leftsubnet are the same inet family
	 * Currently, these tests are implicitely done, and wrongly
	 * in case of 6in4 and 4in6 tunnels
	 */

	if (conn->options_set[KBF_AUTO])
		conn->desired_state = conn->options[KBF_AUTO];

	return err;
}
Example #8
0
int main(int argc, char **argv){

  int               Num_procs;  /* Number of ranks                                */
  int               my_ID;      /* rank                                           */
  int               root=0;
  int               iter, r;    /* dummies                                        */
  int               lsize;      /* logarithmic linear size of grid                */
  int               lsize2;     /* logarithmic size of grid                       */
  int               size;       /* linear size of grid                            */
  s64Int            size2;      /* matrix order (=total # points in grid)         */
  int               radius,     /* stencil parameters                             */
                    stencil_size; 
  s64Int            row, col, first, last; /* dummies                             */
  u64Int            i, j;       /* dummies                                        */
  int               iterations; /* number of times the multiplication is done     */

  s64Int            elm;        /* sequence number of matrix nonzero              */
  s64Int            elm_start;  /* auxiliary variable                             */
  int               jstart,     /* active grid rows parameters                    */
                    jend,
                    nrows,
                    row_offset;
  s64Int            nent;       /* number of nonzero entries                      */
  double            sparsity;   /* fraction of non-zeroes in matrix               */
  double            local_sparse_time,/* timing parameters                        */
                    sparse_time, 
                    avgtime;
  double * RESTRICT matrix;     /* sparse matrix entries                          */
  double * RESTRICT vector;     /* vector multiplying the sparse matrix           */
  double * RESTRICT result;     /* computed matrix-vector product                 */
  double            temp;       /* temporary scalar storing reduction data        */
#ifdef TESTDENSE
  double * RESTRICT rhs;        /* known matrix-vector product                    */
  double * RESTRICT dense;      /* dense matrix equivalent of "matrix"            */
#endif
  double            vector_sum; /* checksum of result                             */
  double            reference_sum, /* local checksum of "rhs"                     */
                    check_sum;  /* aggregate checksum of "rhs"                    */
  double            epsilon = 1.e-8; /* error tolerance                           */
  s64Int * RESTRICT colIndex;   /* column indices of sparse matrix entries        */
  int               error=0;    /* error flag                                     */
  size_t            vector_space, /* variables used to hold malloc sizes          */
                    matrix_space,
                    index_space;
  int               procsize;   /* number of ranks per OS process                 */

/*********************************************************************
** Initialize the MPI environment
*********************************************************************/
  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_ID);
  MPI_Comm_size(MPI_COMM_WORLD, &Num_procs);

/*********************************************************************
** process, test and broadcast input parameters
*********************************************************************/

  if (my_ID == root){
    if (argc != 4){
      printf("Usage: %s <# iterations> <2log grid size> <stencil radius>\n",*argv);
      error = 1;
      goto ENDOFTESTS;
    }

    iterations = atoi(*++argv);
    if (iterations < 1){
      printf("ERROR: Iterations must be positive : %d \n", iterations);
      error = 1;
      goto ENDOFTESTS;
    }

    lsize = atoi(*++argv);
    if (lsize <0) {
      printf("ERROR: Log of grid size must be non-negative: %d\n", 
           (int) lsize);
      error = 1;
      goto ENDOFTESTS;
    }
    lsize2 = 2*lsize;
    size = 1<<lsize;
    if (size < Num_procs) {
      printf("ERROR: Grid size %d must be at least equal to # procs %d\n",
             (int) size, Num_procs);
      error = 1;
      goto ENDOFTESTS;
    }

    if ((int)(size%Num_procs)) {
      printf("ERROR: Grid size %d must be multiple of # procs %d\n", 
             (int) size, Num_procs);
      error = 1;
      goto ENDOFTESTS;
    } 

    /* compute number of points in the grid                                         */
    size2 = size*size;

    radius = atoi(*++argv);
    if (radius <0) {
      printf("ERROR: Stencil radius must be non-negative: %d\n", radius);
      error = 1;
      goto ENDOFTESTS;
    }

    /* emit error if (periodic) stencil overlaps with itself                        */
    if (size <2*radius+1) {
      printf("ERROR: Grid extent %d smaller than stencil diameter 2*%d+1= %d\n",
             size, radius, radius*2+1);
      error = 1;
      goto ENDOFTESTS;
    }
 
    /* sparsity follows from number of non-zeroes per row                           */
    sparsity = (double)(4*radius+1)/(double)size2;

    MPIX_Get_collocated_size(&procsize);
    printf("FG_MPI Sparse matrix-vector multiplication\n");
    printf("Number of ranks          = "FSTR64U"\n", Num_procs);
    printf("Number of ranks/process  = "FSTR64U"\n", procsize);
    printf("Matrix order             = "FSTR64U"\n", size2);
    printf("Stencil diameter         = %16d\n", 2*radius+1);
    printf("Sparsity                 = %16.10lf\n", sparsity);
    printf("Number of iterations     = %16d\n", iterations);
#ifdef SCRAMBLE
    printf("Using scrambled indexing\n");
#else
    printf("Using canonical indexing\n");
#endif

    ENDOFTESTS:;
  }
  bail_out(error);

  /* Broadcast benchmark data to all ranks */
  MPI_Bcast(&lsize,      1, MPI_INT,           root, MPI_COMM_WORLD);
  MPI_Bcast(&lsize2,     1, MPI_INT,           root, MPI_COMM_WORLD);
  MPI_Bcast(&size,       1, MPI_LONG_LONG_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&size2,      1, MPI_LONG_LONG_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&radius,     1, MPI_INT,           root, MPI_COMM_WORLD);
  MPI_Bcast(&iterations, 1, MPI_INT,           root, MPI_COMM_WORLD);

  /* compute total size of star stencil in 2D                                     */
  stencil_size = 4*radius+1;
  /* compute number of rows owned by each rank                                    */
  nrows = size2/Num_procs;

  /* compute total number of non-zeroes for this rank                             */
  nent = nrows*stencil_size;

  matrix_space = nent*sizeof(double);
  if (matrix_space/sizeof(double) != nent) {
    printf("ERROR: rank %d cannot represent space for matrix: %ul\n", 
           my_ID, matrix_space);
    error = 1;
  } 
  bail_out(error);

  matrix = (double *) malloc(matrix_space);
  if (!matrix) {
    printf("ERROR: rank %d could not allocate space for sparse matrix: "FSTR64U"\n", 
           my_ID, matrix_space);
    error = 1;
  } 
  bail_out(error);

  vector_space = (size2 + nrows)*sizeof(double);
  if (vector_space/sizeof(double) != (size2+nrows)) {
    printf("ERROR: rank %d Cannot represent space for vectors: %ul\n", 
           my_ID, vector_space);
    error = 1; 
  } 
  bail_out(error);

  vector = (double *) malloc(vector_space);
  if (!vector) {
    printf("ERROR: rank %d could not allocate space for vectors: %d\n", 
           my_ID, (int)(2*nrows));
    error = 1;
  }
  bail_out(error);
  result = vector + size2;

  index_space = nent*sizeof(s64Int);
  if (index_space/sizeof(s64Int) != nent) {
    printf("ERROR: rank %d cannot represent space for column indices: %ul\n", 
           my_ID, index_space);
    error = 1;
  } 
  bail_out(error);

  colIndex = (s64Int *) malloc(index_space);
  if (!colIndex) {
    printf("ERROR: rank %d Could not allocate space for column indices: "FSTR64U"\n",
           my_ID, nent*sizeof(s64Int));
    error = 1;
  } 
  bail_out(error);

  /* fill matrix with nonzeroes corresponding to difference stencil. We use the 
     scrambling for reordering the points in the grid.                            */

  jstart = (size/Num_procs)*my_ID;
  jend   = (size/Num_procs)*(my_ID+1);

  for (j=jstart; j<jend; j++) for (i=0; i<size; i++) {
    elm_start = (i+(j-jstart)*size)*stencil_size;
    elm = elm_start;
    colIndex[elm] = REVERSE(LIN(i,j),lsize2);
    for (r=1; r<=radius; r++, elm+=4) {
      colIndex[elm+1] = REVERSE(LIN((i+r)%size,j),lsize2);
      colIndex[elm+2] = REVERSE(LIN((i-r+size)%size,j),lsize2);
      colIndex[elm+3] = REVERSE(LIN(i,(j+r)%size),lsize2);
      colIndex[elm+4] = REVERSE(LIN(i,(j-r+size)%size),lsize2);
    }
    /* sort colIndex to make sure the compressed row accesses
       vector elements in increasing order                                        */
    qsort(&(colIndex[elm_start]), stencil_size, sizeof(s64Int), compare);
    for (elm=elm_start; elm<elm_start+stencil_size; elm++) 
      matrix[elm] = 1.0/(double)(colIndex[elm]+1);   
  }

#if defined(TESTDENSE) && defined(VERBOSE)
  /* fill dense matrix to test                                                    */
  matrix_space = size2*size2/Num_procs*sizeof(double);
  if (matrix_space/sizeof(double) != size2*size2/Num_procs) {
    printf("ERROR: Cannot represent space for matrix: %ul\n", matrix_space);
    exit(EXIT_FAILURE);
  } 
  dense = (double *) malloc(matrix_space);
  if (!dense) {
    printf("ERROR: Could not allocate space for dense matrix of order: %d\n",
           (int) size2);
    exit(EXIT_FAILURE);
  }
  rhs   = (double *) malloc(vector_space);
  if (!rhs) {
    printf("ERROR: Could not allocate space for rhs: %d\n", (int) size2);
    exit(EXIT_FAILURE);
  }
  for (row=0; row<nrows; row++) {
    for (col=0; col<size2; col++) DENSE(col,row) = 0.0;
    first = row*stencil_size; last = first+stencil_size-1;
    rhs[row] = (double) (last-first+1) * (double) iterations;
    for (elm=first; elm<=last; elm++) DENSE(colIndex[elm],row) = matrix[elm];
  }
#endif

  /* initialize the input and result vectors                                      */
  for (row=0; row<nrows; row++) result[row] = vector[row] = 0.0;

  for (iter=0; iter<=iterations; iter++) {

    /* start timer after a warmup iteration */
    if (iter == 1) { 
      MPI_Barrier(MPI_COMM_WORLD);
      local_sparse_time = wtime();
    }

    /* fill vector                                                                */
    row_offset = nrows*my_ID;
    for (row=row_offset; row<nrows+row_offset; row++) vector[row] += (double) (row+1);

    /* replicate vector on all rankors                                         */
    MPI_Allgather(MPI_IN_PLACE, nrows, MPI_DOUBLE, vector, nrows, MPI_DOUBLE,
                  MPI_COMM_WORLD);

    /* do the actual matrix multiplication                                        */
    for (row=0; row<nrows; row++) {
      first = stencil_size*row; last = first+stencil_size-1;
      #pragma simd reduction(+:temp) 
      for (temp=0.0,col=first; col<=last; col++) {
        temp += matrix[col]*vector[colIndex[col]];
      }
      result[row] += temp;
    }
  } /* end of iterations                                                          */

  local_sparse_time = wtime() - local_sparse_time;
  MPI_Reduce(&local_sparse_time, &sparse_time, 1, MPI_DOUBLE, MPI_MAX, root,
             MPI_COMM_WORLD);


#if defined(TESTDENSE) && defined(VERBOSE)
  /* print matrix, vector, rhs, plus computed solution                            */
  for (row=0; row<nrows; row++) {
    printf("( ");
    for (col=0; col<size2; col++) printf("%1.3lf  ", DENSE(col,row));
    printf(" ) ( %1.3lf ) = ( %1.3lf ) | ( %1.3lf )\n", 
           vector[row], result[row], rhs[row]);
  }
#endif

  /* verification test                                                            */
  reference_sum = 0.5 * (double) size2 * (double) stencil_size * 
    (double) (iterations+1) * (double) (iterations + 2);

  vector_sum = 0.0;
  for (row=0; row<nrows; row++) vector_sum += result[row];

  MPI_Reduce(&vector_sum, &check_sum, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);

  if (my_ID == root) {
    if (ABS(check_sum-reference_sum) > epsilon) {
      printf("ERROR: Vector sum = %lf, Reference vector sum = %lf, my_ID = %d\n",
             check_sum, reference_sum, my_ID);
      error = 1;
    }
    else {
      printf("Solution validates\n");
#ifdef VERBOSE
      printf("Reference sum = %lf, check sum = %lf\n", 
             reference_sum, check_sum);
#endif
    }
    avgtime = sparse_time/iterations;
    printf("Rate (MFlops/s): %lf  Avg time (s): %lf\n",
           1.0E-06 * (2.0*nent*Num_procs)/avgtime, avgtime);
  }

  bail_out(error);

  MPI_Finalize();
  exit(EXIT_SUCCESS);
}
double* cholesky(double *A, int n) {
    register int i, j, k;
    double *L = (double*)calloc(n * n, sizeof(double));
    for (i = 0; i < n; ++i) {
        for (j = 0; j < i; ++j) {
            L[LIN(i, j)] = A[IN(i, j)];
            for (k = 0; k < j; ++k) {
                L[LIN(i, j)] -= L[LIN(i, k)] * L[LIN(j, k)];
            }

            L[LIN(i, j)] /= L[LIN(j, j)];
        }

        L[LIN(j, j)] = A[IN(j, j)];
        for (k = 0; k < i; ++k) {
            L[LIN(i, i)] -= L[LIN(i, k)] * L[LIN(i, k)];
        }
       
        L[LIN(i, i)] = sqrt(L[LIN(i, i)]);
    }

    return L;
} 
Example #10
0
File: hex.c Project: mrahn/hex
static int not_seen (PState_DFS state, int x, int y)
{
  return !state->seen[LIN (x, y)];
}
Example #11
0
File: hex.c Project: mrahn/hex
static void push (PState_DFS state, int x, int y)
{
  state->open[state->end++] = x;
  state->open[state->end++] = y;
  state->seen[LIN (x, y)] = 1;
}