int main(int argc,char *argv[])
{
    pthread_mutex_init(&lock,NULL);
    if(argc==1)
    {
        nRows=nCols=1000;
        nThread=100;
    }
    if(argc==2)
    {
        nRows=nCols=atoi(argv[1]);
        nThread=100;
    }

    if(argc==4)
    {
        nRows=atoi(argv[1]);
        nCols=atoi(argv[2]);
        nThread=atoi(argv[3]);
    }
    int i,j;
    MatrixA=(int **)malloc(sizeof(int*)*nRows);
    for(i=0; i<nRows; i++)
        MatrixA[i]=(int *)malloc(sizeof(int)*nCols);
    MatrixB=(int **)malloc(sizeof(int*)*nCols);
    for(i=0; i<nCols; i++)
        MatrixB[i]=(int *)malloc(sizeof(int)*nRows);
    MatrixC=(int **)malloc(sizeof(int*)*nRows);
    for(i=0; i<nRows; i++)
        MatrixC[i]=(int *)malloc(sizeof(int)*nRows);
    fillMatrix(MatrixA,nRows,nCols);
    fillMatrix(MatrixB,nCols,nRows);
    rowthread=(pthread_t*)malloc(sizeof(pthread_t)*nThread);
//colthread=(pthread_t*)malloc(sizeof(pthread_t)*nThread);
//calculationthread=(pthread_t*)malloc(sizeof(pthread_t)*nThread);
    printf_matrix(MatrixA,nRows,nCols);
    printf_matrix(MatrixB,nRows,nCols);
//printf("\n  starting ---------------\n");
    for(i=0; i<nThread; i++)
    {
        printf("\n created ---\n");
        int a=pthread_create(&rowthread[i],NULL,RowMultiply,&i);
        if(a!=0)
        {

            printf("Thread Creation Error..\n");
            exit(1);
        }
    }
    for(i=0; i<nThread; i++)
        pthread_join(rowthread[i],NULL);

    printf_matrix(MatrixC,nRows,nRows);
    free(MatrixA);
    free(MatrixB);
    free(MatrixC);
    return 0;
}
Esempio n. 2
0
void inference(char* dataset, char* model_root, char* out)
{
    int i;
    char fname[100];

    // read the data and model
    corpus * corpus = read_data(dataset);
    llna_model * model = read_llna_model(model_root);
    gsl_vector * lhood = gsl_vector_alloc(corpus->ndocs);
    gsl_matrix * corpus_nu = gsl_matrix_alloc(corpus->ndocs, model->k);
    gsl_matrix * corpus_lambda = gsl_matrix_alloc(corpus->ndocs, model->k);
    // gsl_matrix * topic_lhoods = gsl_matrix_alloc(corpus->ndocs, model->k);
    gsl_matrix * phi_sums = gsl_matrix_alloc(corpus->ndocs, model->k);

    // approximate inference
    init_temp_vectors(model->k-1); // !!! hacky
    sprintf(fname, "%s-word-assgn.dat", out);
    FILE* word_assignment_file = fopen(fname, "w");
    for (i = 0; i < corpus->ndocs; i++)
    {
        doc doc = corpus->docs[i];
        llna_var_param * var = new_llna_var_param(doc.nterms, model->k);
        init_var_unif(var, &doc, model);

        vset(lhood, i, var_inference(var, &doc, model));
        gsl_matrix_set_row(corpus_lambda, i, var->lambda);
        gsl_matrix_set_row(corpus_nu, i, var->nu);
        gsl_vector curr_row = gsl_matrix_row(phi_sums, i).vector;
        col_sum(var->phi, &curr_row);
        write_word_assignment(word_assignment_file, &doc, var->phi);

        printf("document %05d, niter = %05d\n", i, var->niter);
        free_llna_var_param(var);
    }

    // output likelihood and some variational parameters
    sprintf(fname, "%s-ctm-lhood.dat", out);
    printf_vector(fname, lhood);
    sprintf(fname, "%s-lambda.dat", out);
    printf_matrix(fname, corpus_lambda);
    sprintf(fname, "%s-nu.dat", out);
    printf_matrix(fname, corpus_nu);
    sprintf(fname, "%s-phi-sum.dat", out);
    printf_matrix(fname, phi_sums);

}
Esempio n. 3
0
int main( void )
{
	int i, j, c;
	unsigned long step;
	char matrix[MAX][MAX];
	char class;

	/*初始化随机数发生器、matrix矩阵、Site结构体*/
	srand((unsigned)time(0));
	Point Site;
	
	/*初始化矩阵matrix,填入顺序的数字标号*/
	printf("I'm initializing...\n");

	for(i = 0, class = CLASS; i < MAX; i++)
		for(j = 0; j < MAX; j++, class++)
			matrix[i][j]= class;
	matrix[MAX - 1][MAX - 1] = ' ';

	/*随机移动矩阵matrix中的数字标号,作为游戏起始状态*/
	for(step = 0; step < MAX_STEP; step++) {
		c = roll(4);
		Site = find_point( &matrix[0] );
		switch(c) {
		case 0:
			if((Site.y + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y + 1];
				matrix[Site.x][Site.y + 1] = ' ';
			} break;
		case 1:
			if((Site.y - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y - 1];
				matrix[Site.x][Site.y - 1] = ' ';
			} break;
		case 2:
			if((Site.x - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x - 1][Site.y];
				matrix[Site.x - 1][Site.y] = ' ';
			} break;
		case 3:
			if((Site.x + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x + 1][Site.y];
				matrix[Site.x + 1][Site.y] = ' ';
			} break;
		}		
	}
	printf_matrix( &matrix[0] );
	printf("I'm working...\n");

	/*游戏部分,从键盘接收命令,执行相应的动作,并打印出矩阵*/
	for(step = 0; is_win(&matrix[0]) != 1; step++) {
		c = my_getch();
		Site = find_point( &matrix[0] );
		switch(c) {
		case 'a':
			if((Site.y + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y + 1];
				matrix[Site.x][Site.y + 1] = ' ';
			} break;
		case 'd':
			if((Site.y - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y - 1];
				matrix[Site.x][Site.y - 1] = ' ';
			} break;
		case 's':
			if((Site.x - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x - 1][Site.y];
				matrix[Site.x - 1][Site.y] = ' ';
			} break;
		case 'w':
			if((Site.x + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x + 1][Site.y];
				matrix[Site.x + 1][Site.y] = ' ';
			} break;
		case 'Q':
			printf("Used %lu steps.\n", step);
			exit(0);
		}		
        system("clear");
		printf_matrix( &matrix[0] );
	    printf("Used %lu steps.\n", step);
	}

	/*打印出总共执行的步数step*/
    system("clear");
	printf_matrix( &matrix[0] );
	printf("I win!\n");
	printf("Used %lu steps.\n", step);
	return 0;
}
Esempio n. 4
0
// load a .mat matlab matrix
// will load the first matrix (sparse or dense) in the file
// returns 0 on success
static
  int read_mat(char const *const filename,
               matrix_t *const A) {
#ifndef HAVE_MATIO
  return 1;
#else
  const int LOCAL_DEBUG = 0;
  mat_t* matfp;
  matfp = Mat_Open(filename, MAT_ACC_RDONLY);
  if(matfp == NULL)
    return 1; // failed to open file

  matvar_t* t;
  int more_data = 1;

  while (more_data) {
    t = Mat_VarReadNextInfo(matfp);
    if(t == NULL) {
      return 2; // no suitable variable found
    }
// TODO decide if this is the one:    if(t->
    if(1) {
      more_data = 0;
    }
    else { // keep going
      Mat_VarFree(t);
      t = NULL;
    }
  }

  { // load the selected variable, including data
    Mat_Rewind(matfp);
    matvar_t* tt = Mat_VarRead(matfp, t->name);
    Mat_VarFree(t);
    t = tt;
    if(LOCAL_DEBUG) Mat_VarPrint(tt, 1);
  }

  // debug info
  if(LOCAL_DEBUG && t->name != NULL)
    printf("%s: loaded variable %s\n", filename, t->name);

  // checks and data handling
  int ret = 0;

  if(t->rank > 2 || t->rank <= 0) { // number of dimensions
    ret = 2;
  }
  else if(t->data_type != MAT_T_DOUBLE) {
    if(LOCAL_DEBUG) printf("data_type=%d\n",t->data_type);
    ret = 3;
  }
  else if(t->isComplex) {
    ret = 10; // TODO can't handle complex matrices yet
  }
  else if(t->isLogical) {
    ret = 11; // TODO can't handle logicals yet
  }
  else {
    if(t->rank == 1) {
      A->m = t->dims[0]; // rows
      A->n = 1; // cols
    }
    else {
      A->m = t->dims[0]; // rows
      A->n = t->dims[1]; // cols
    }
    A->sym = SM_UNSYMMETRIC;
    //if(t->data_type == MAT_T_DOUBLE) {
    A->data_type = REAL_DOUBLE;
    //} // TODO complex, single precision, various sized integers

    if(t->class_type == MAT_C_SPARSE) { // t.data = sparse_t in CSC format
      // Note that Matlab will save('-v4'...) a sparse matrix
      // to version 4 format without complaint but it appears
      // to be gibberish as far as MatIO is concerned
      sparse_t* st = t->data;
      A->nz = st->ndata; //st->nzmax has the actual size of the allocated st->data
      A->format = SM_CSC;
      // transfer the data pointer into our strucut
      // TODO check for negative values in ir/jc before throwing away their signs
      A->ii = (unsigned int*) st->ir;
      st->ir = NULL;
      A->jj = (unsigned int*) st->jc;
      st->jc = NULL;
      A->dd = st->data;
      st->data = NULL;
    }
    else if(t->class_type == MAT_C_DOUBLE) {
      A->nz = A->m * A->n;
      A->format = DCOL;
      // transfer the data pointer into our struct
      A->dd = t->data;
      t->data = NULL;
    }
    else {
      ret = 4; // unknown class of data structure
    }
  }

  // DEBUG
  if(LOCAL_DEBUG && validate_matrix(A) != 0) {
    ret = 50;
    fprintf(stderr, "problem loading .mat matrix file\n");
    printf_matrix("  ", A);
  }

  // sanity checks
  // t.dims[] is don't-care
  // t.isGlobal is don't-care

  Mat_Close(matfp);
  Mat_VarFree(t);
  return ret; // success?
#endif
}
Esempio n. 5
0
void em(char* dataset, int k, char* start, char* dir)
{
    FILE* lhood_fptr;
    char string[100];
    int iteration;
    double convergence = 1, lhood = 0, lhood_old = 0;
    corpus* corpus;
    llna_model *model;
    llna_ss* ss;
    time_t t1,t2;
    double avg_niter, converged_pct, old_conv = 0;
    gsl_matrix *corpus_lambda, *corpus_nu, *corpus_phi_sum;
    short reset_var = 1;

    // read the data and make the directory

    corpus = read_data(dataset);
    mkdir(dir, S_IRUSR|S_IWUSR|S_IXUSR);

    // set up the log likelihood log file

    sprintf(string, "%s/likelihood.dat", dir);
    lhood_fptr = fopen(string, "w");

    // run em

    model = em_initial_model(k, corpus, start);
    ss = new_llna_ss(model);
    corpus_lambda = gsl_matrix_alloc(corpus->ndocs, model->k);
    corpus_nu = gsl_matrix_alloc(corpus->ndocs, model->k);
    corpus_phi_sum = gsl_matrix_alloc(corpus->ndocs, model->k);
    time(&t1);
    init_temp_vectors(model->k-1); // !!! hacky
    iteration = 0;
    sprintf(string, "%s/%03d", dir, iteration);
    write_llna_model(model, string);
    do
    {
        printf("***** EM ITERATION %d *****\n", iteration);

        expectation(corpus, model, ss, &avg_niter, &lhood,
                    corpus_lambda, corpus_nu, corpus_phi_sum,
                    reset_var, &converged_pct);
        time(&t2);
        convergence = (lhood_old - lhood) / lhood_old;
        fprintf(lhood_fptr, "%d %5.5e %5.5e %5ld %5.5f %1.5f\n",
                iteration, lhood, convergence, (int) t2 - t1, avg_niter, converged_pct);

        if (((iteration % PARAMS.lag)==0) || isnan(lhood))
        {
            sprintf(string, "%s/%03d", dir, iteration);
            write_llna_model(model, string);
            sprintf(string, "%s/%03d-lambda.dat", dir, iteration);
            printf_matrix(string, corpus_lambda);
            sprintf(string, "%s/%03d-nu.dat", dir, iteration);
            printf_matrix(string, corpus_nu);
        }
        time(&t1);

        if (convergence < 0)
        {
            reset_var = 0;
            if (PARAMS.var_max_iter > 0)
                PARAMS.var_max_iter += 10;
            else PARAMS.var_convergence /= 10;
        }
        else
        {
            maximization(model, ss);
            lhood_old = lhood;
            reset_var = 1;
            iteration++;
        }

        fflush(lhood_fptr);
        reset_llna_ss(ss);
        old_conv = convergence;
    }
    while ((iteration < PARAMS.em_max_iter) &&
           ((convergence > PARAMS.em_convergence) || (convergence < 0)));

    sprintf(string, "%s/final", dir);
    write_llna_model(model, string);
    sprintf(string, "%s/final-lambda.dat", dir);
    printf_matrix(string, corpus_lambda);
    sprintf(string, "%s/final-nu.dat", dir);
    printf_matrix(string, corpus_nu);
    fclose(lhood_fptr);
}