Пример #1
0
void init_trains(){
  const int ICT_trainLength = 8;
  const int ICT_trainBoxBufSize = 2*1024*1024;
  int i;
  
  for(i=0; i< TRAIN_BUF_NUM; i++){
	initTrain( &(hx_train[i]), ICT_trainLength, ICT_trainBoxBufSize);
	printf("init train %d=%p\n", i, &(hx_train[i]));
  }
}
Пример #2
0
int main ( int argc, char *argv[] )
{
  int		trainCount = 0;
  char 		*filename = NULL;
  pthread_t	*tids;
  int		i;


  /* Parse the arguments */
  if ( argc < 2 )
  {
    printf ("Usage: part1 n {filename}\n\t\tn is number of trains\n");
    printf ("\t\tfilename is input file to use (optional)\n");
    exit(0);
  }

  if ( argc >= 2 )
  {
    trainCount = atoi(argv[1]);
  }
  if ( argc == 3 )
  {
    filename = argv[2];
  }	

  initTrain(filename);
  tids = (pthread_t *) malloc(sizeof(pthread_t)*trainCount);

  for (i=0;i<trainCount;i++)
  {
    TrainInfo *info = createTrain();

    if ( pthread_create (&tids[i],0, Train, (void *)info) != 0 )
    {
      printf ("Failed creation of Train.\n");
      exit(0);
    }
  }

  /*
   * This code waits for all train threads to terminate
   */
  for (i=0;i<trainCount;i++)
  {
    pthread_join (tids[i], NULL);
  }

  free(tids);
  free(East);
  free(West);
  return 0;
}
Пример #3
0
	void TestDataSource::initTrains()
	{
		switch (test) {
		case 1:
			// Tests basic train switch cost logic. Expected result is to ride train 1 all the way to station 600, 
			// even though some of the route appears to be faster if we switch train.
			// run with -t 1 100 10:00:00 600
			initTrain(3, stop_arr_t{
				  makeStop(500, "12:01:00"), makeStop(600, "12:30:00")
		});
			initTrain(1, stop_arr_t{
				makeStop(100, "10:00:00"), makeStop(200, "10:30:00"), makeStop(300, "11:00:00"), makeStop(400, "11:30:00"), makeStop(500, "12:00:00"), makeStop(600, "12:30:00")
			});
			initTrain(2, stop_arr_t{
				makeStop(200, "10:31:00"), makeStop(400, "10:32:00")
			});
			break;
		case 2:
			// Basic shortest-path test, expected result is to ride train 2 from 100 to 400 and then go back to 300 using train 3
			// run with -t 2 100 10:00:00 300
			initTrain(1, stop_arr_t{
				  makeStop(100, "10:00:00"), makeStop(200, "10:30:00"), makeStop(300, "11:00:00"), makeStop(400, "11:30:00")
		});
			initTrain(2, stop_arr_t{
				makeStop(100, "10:00:00"), makeStop(400, "10:30:00")
			});
			initTrain(3, stop_arr_t{
				makeStop(400, "10:30:00"), makeStop(300, "10:40:00")
			});
			break;
		case 3:
			// Test train switch minimization in a more complicated case. Expected result is to only use train 1.
			// run with -t 3 100 10:00:00 400
			initTrain(1, stop_arr_t{
				  makeStop(100, "10:10:00"), makeStop(200, "10:30:00"), makeStop(300, "11:00:00"), makeStop(400, "11:30:00")
		});
			initTrain(2, stop_arr_t{
				makeStop(100, "10:00:00"), makeStop(300, "10:30:00")
			});
			break;
		case 4:
			// Test trains with WAIT_ON_TRAIN and alt-route finding
			// expected result is to use train 2 and then switch to 3
			// run with -t 4 100 10:00 300
			createTrain(1, getStationById(100), getStationById(200), Utils::parseTime("10:00"), Utils::parseTime("10:20"));
			createTrain(1, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("11:00"));
			
			createTrain(2, getStationById(100), getStationById(200), Utils::parseTime("10:10"), Utils::parseTime("10:20"));
			
			createTrain(3, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("10:40"));
			break;
		case 5:
			// Test trains with WAIT_ON_TRAIN
			// expected result is to use train 1 only in train switching mode, train 2 then 1 in delayed leaving
			// run with -t 5 100 10:00 300
			createTrain(1, getStationById(100), getStationById(200), Utils::parseTime("10:00"), Utils::parseTime("10:20"));
			createTrain(1, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("11:00"));

			createTrain(2, getStationById(100), getStationById(200), Utils::parseTime("10:10"), Utils::parseTime("10:20"));

			createTrain(3, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("11:00"));
			break;
		default:
			throw HaException("Test case not implemented", HaException::UNIMPLEMENTED_ERROR);
		}
	}
Пример #4
0
void train() {
	/* initialize output */
	printf("init train\n");
	initTrain();

	/* initialize temp variables */
	double *myalpha_new = (double *) malloc(sizeof(double)*K);
	double *psi_sum_beta = (double *) malloc(sizeof(double)*M);
	double *psi_myalpha = (double *) malloc(sizeof(double)*K);
	double **log_myrho = (double **) malloc(sizeof(double*)*M);
	double **psi_mybeta = (double **) malloc(sizeof(double*)*M);
	for (int m = 0; m < M; m++) {
		log_myrho[m] = (double *) malloc(sizeof(double)*K);
		psi_mybeta[m] = (double *) malloc(sizeof(double)*K);
	}
	double **old_mytheta = (double **) malloc(sizeof(double*)*K);
	double **log_mytheta = (double **) malloc(sizeof(double*)*K);
	double **log_inv_mytheta = (double **) malloc(sizeof(double*)*K);
	for (int k = 0; k < K; k++) {
		old_mytheta[k] = (double *) malloc(sizeof(double)*L);
		for (int l = 0; l < L; l++) old_mytheta[k][l] = 0;
		log_mytheta[k] = (double *) malloc(sizeof(double)*L);
		log_inv_mytheta[k] = (double *) malloc(sizeof(double)*L);
	}
	double *g = (double *) malloc(sizeof(double)*K);
	double *q = (double *) malloc(sizeof(double)*K);

	double maxDiff = 0;

	for (int out_iter = 0; out_iter < OUT_LOOP; out_iter++) {
		if (out_iter % 100 == 0) printf("Iter: %d\n", out_iter);
		for (int k = 0; k < K; k++) {
			for (int l = 0; l < L; l++) {
#ifdef NEW_PRIOR
				if (A[l] == 0) continue;
#endif
				log_mytheta[k][l] = log(mytheta[k][l]);
				//printf("%lf ", log(mytheta[k][l]));
				log_inv_mytheta[k][l] = log(1-mytheta[k][l]);
			}
			//printf("\n");
		}
		/* e-step */
//		for (int in_iter = 0; in_iter < IN_LOOP; in_iter++) {
			//printf("in iter: %d\n", in_iter);
#pragma omp parallel shared(M,N,K,L,mybeta,psi_mybeta,log_myrho,log_mytheta,log_inv_mytheta,r)
			{
#pragma omp for schedule(dynamic,1)
				for (int m = 0; m < M; m++) {
					/* computer r */
					double sum_beta = 0;
					for (int k = 0; k < K; k++) {
						sum_beta += mybeta[m][k];
						psi_mybeta[m][k] = DiGamma_Function(mybeta[m][k]);
					}
					psi_sum_beta[m] = DiGamma_Function(sum_beta);
					for (int n = 0; n < N[m]; n++) {
						for (int k = 0; k < K; k++) {
							log_myrho[m][k] = psi_mybeta[m][k]-psi_sum_beta[m];
							for (int l = 0; l < L; l++) {
#ifdef NEW_PRIOR
								if (A[l] == 0) continue;
#endif

								if (R[m][n][l]) {
									log_myrho[m][k] += log_mytheta[k][l];
								} else {
									log_myrho[m][k] += log_inv_mytheta[k][l];
								}
							}
						}
						double log_sum_rho = logsumexp(log_myrho[m], K);
						for (int k = 0; k < K; k++) {
							r[m][n][k] = exp(log_myrho[m][k] - log_sum_rho);
						}
					}

					/* compute mybeta */
					for (int k = 0; k < K; k++) {
						mybeta[m][k] = myalpha[k];
						for (int n = 0; n < N[m]; n++) {
							mybeta[m][k] = mybeta[m][k] + r[m][n][k];
						}
					}
				}
			}
			/*
			printf("beta:\n");
			for (int m = 0; m < M; m++) {
				for (int k = 0; k < K; k++) {
					printf("%lf ", mybeta[m][k]);
				}
				printf("\n");
			}
			*/
//		}
#ifdef DEBUG
		printf("beta:\n");
		for (int m = 0; m < M; m++) {
			for (int k = 0; k < K; k++) {
				printf("%lf ", mybeta[m][k]);
			}
			printf("\n");
		}
#endif
		/* m-step */
		if (out_iter != OUT_LOOP - 1) {
			/* update alpha */
			if (mode == UPDATE_ALPHA) {
				for (int m = 0; m < M; m++) {
					double sum_beta = 0;
					for (int k = 0; k < K; k++) {
						sum_beta += mybeta[m][k];
						psi_mybeta[m][k] = DiGamma_Function(mybeta[m][k]);
					}
					psi_sum_beta[m] = DiGamma_Function(sum_beta);
				}
				int converge = 0;
				for (int iter = 0; iter < 1000; iter++) {
					double sum_alpha = 0;
					for (int k = 0; k < K; k++) {
						sum_alpha += myalpha[k];
						psi_myalpha[k] = DiGamma_Function(myalpha[k]);
					}
					double psi_sum_alpha = DiGamma_Function(sum_alpha);
					int fault;
					for (int k = 0; k < K; k++) {
						g[k] = M * (psi_sum_alpha - psi_myalpha[k]);
						for (int m = 0; m < M; m++) {
							g[k] += psi_mybeta[m][k] - psi_sum_beta[m];
						}
						q[k] = -M * trigamma(myalpha[k], &fault);
					}
					double z = M * trigamma(sum_alpha, &fault);
					double gq = 0;
					double rq = 0;
					for (int k = 0; k < K; k++) {
						gq = gq + g[k] / q[k];
						rq = rq + 1 / q[k];
					}
					double b = gq / (1 / z + rq);
					for (int k = 0; k < K; k++) {
						myalpha_new[k] = myalpha[k] - (g[k] - b) / q[k];
						if (myalpha_new[k] < 0) {
							printf("warning alpha small than zero\n");
						}
					}
#ifdef DEBUG
					printf("alpha:\n");
					for (int k = 0; k < K; k++) {
						printf("%lf ", myalpha[k]);
					}
					printf("\n");
#endif

					converge = 1; 
					for (int k = 0; k < K; k++) {
						double diff = myalpha_new[k] - myalpha[k];
						if (diff > 1e-6 || diff < -1e-6) {
							converge = 0;
							break;
						}
					}
					if (converge) {
						break;
					}

					double *tmpalpha = myalpha;
					myalpha = myalpha_new;
					myalpha_new = tmpalpha;
				}
				if (!converge) {
					printf("warning: not converge\n");
				}
			}

			/* update theta */
#pragma omp parallel shared(K,N,L,M,mytheta,r,R)
			{
#pragma omp for schedule(dynamic,1)
				for (int k = 0; k < K; k++) {
					for (int l = 0; l < L; l++) {
						double rR = 0;
						double sum_r = 0;
#ifdef PRIOR
						rR += A;
						sum_r += A + B;
#endif

#ifdef NEW_PRIOR
						if (A[l] == 0) continue;
						rR += A[l];
						sum_r += A[l] + B[l];
#endif

						for (int m = 0; m < M; m++) {
							for (int n = 0; n < N[m]; n++) {
								rR += r[m][n][k]*R[m][n][l];
								sum_r += r[m][n][k];
							}
						}
						mytheta[k][l] = rR / sum_r;
						if (EQUAL(rR,0.0)) {
							mytheta[k][l] = 0;
						}
						if (mytheta[k][l] < 0 || mytheta[k][l] > 1 || mytheta[k][l] != mytheta[k][l]) {
							printf("error %lf %lf\n", rR, sum_r);
						}
					}
				}
			}

			maxDiff = 0;
			for (int k = 0; k < K; k++ ){
				for (int l = 0; l < L; l++) {
#ifdef NEW_PRIOR
					if (A[l] == 0) continue;
#endif
					double diff = old_mytheta[k][l] - mytheta[k][l];
					if (diff > maxDiff) maxDiff = diff;
					if	(-diff > maxDiff) maxDiff = -diff;
					old_mytheta[k][l] = mytheta[k][l];
				}
			}
			if (maxDiff < 1e-6) {
				printf("Finished.\n");
				break;
			}

#ifdef DEBUG
			printf("theta:\n");
			for (int k = 0; k < K; k++) {
				for (int l = 0; l < L; l++) {
					printf("%lf ", mytheta[k][l]);
				}
				printf("\n");
			}
#endif
		}
	}

	/* free temp variables */
	free(g);
	free(q);
	for (int k = 0; k < K; k++) {
		free(log_inv_mytheta[k]);
		free(log_mytheta[k]);
		free(old_mytheta[k]);
	}
	free(old_mytheta);
	free(log_inv_mytheta);
	free(log_mytheta);
	for (int m = 0; m < M; m++) {
		free(psi_mybeta[m]);
		free(log_myrho[m]);
	}
	free(psi_mybeta);
	free(log_myrho);
	free(psi_sum_beta);
	free(psi_myalpha);
	free(myalpha_new);
}
int main ( int argc, char *argv[] )
{
	int		trainCount = 0;
	char 		*filename = NULL;
	pthread_t	*tids;
	int		i;

	
	/* Parse the arguments */
	if ( argc < 2 )
	{
		printf ("Usage: part1 n {filename}\n\t\tn is number of trains\n");
		printf ("\t\tfilename is input file to use (optional)\n");
		exit(0);
	}
	
	if ( argc >= 2 )
	{
		trainCount = atoi(argv[1]);
		westQueue.head = malloc(sizeof(TrainInfo*)*trainCount);
		westQueue.size = 0;
		westQueue.consecutiveTrainsCrossed = 0;
		eastQueue.head = malloc(sizeof(TrainInfo*)*trainCount);
		eastQueue.size = 0;
		eastQueue.consecutiveTrainsCrossed = 0;
	}
	if ( argc == 3 )
	{
		filename = argv[2];
	}	

	if (pthread_mutex_init(&queueLock, NULL) != 0)
    {
        printf("\nInit mutex queueLock failed\n");
        return 1;
    }
	if (pthread_mutex_init(&bridgeLock, NULL) != 0)
    {
        printf("\nInit mutex bridgeLock failed\n");
        return 1;
    }
	if (pthread_cond_init(&synchronizeCond, NULL) != 0)
    {
        printf("\nInit condition synchronizeCond failed\n");
        return 1;
    }
	initTrain(filename, trainCount);
	
	/*
	 * Since the number of trains to simulate is specified on the command
	 * line, we need to malloc space to store the thread ids of each train
	 * thread.
	 */
	tids = (pthread_t *) malloc(sizeof(pthread_t)*trainCount);
	
	/*
	 * Create all the train threads pass them the information about
	 * length and direction as a TrainInfo structure
	 */

	for (i=0;i<trainCount;i++)
	{
		TrainInfo *info = createTrain();

		printf ("Train %2d headed %s length is %d\n", info->trainId,
			(info->direction == DIRECTION_WEST ? "West" : "East"),
			info->length );

		if ( pthread_create (&tids[i],0, Train, (void *)info) != 0 )
		{
			printf ("Failed creation of Train.\n");
			exit(0);
		}
	}

	/*
	 * This code waits for all train threads to terminate
	 */
	for (i=0;i<trainCount;i++)
	{
		pthread_join (tids[i], NULL);
	}
	pthread_mutex_destroy(&queueLock);
	pthread_mutex_destroy(&bridgeLock);
	pthread_cond_destroy(&synchronizeCond);

	free(tids);
	return 0;
}