Example #1
0
File: cg.c Project: akva2/tma4280
int cg(Matrix A, Vector b, double tolerance)
{
  int i=0, j;
  double rl;
  Vector r = createVector(b->len);
  Vector p = createVector(b->len);
  Vector buffer = createVector(b->len);
  double dotp = 1000;
  double rdr = dotp;
  copyVector(r,b);
  fillVector(b, 0.0);
  rl = sqrt(dotproduct(r,r));
  while (i < b->len && rdr > tolerance*rl) {
    ++i;
    if (i == 1) {
      copyVector(p,r);
      dotp = dotproduct(r,r);
    } else {
      double dotp2 = dotproduct(r,r);
      double beta = dotp2/dotp;
      dotp = dotp2;
      scaleVector(p,beta);
      axpy(p,r,1.0);
    }
    MxV(buffer, p);
    double alpha = dotp/dotproduct(p,buffer);
    axpy(b,p,alpha);
    axpy(r,buffer,-alpha);
    rdr = sqrt(dotproduct(r,r));
  }
  freeVector(r);
  freeVector(p);
  freeVector(buffer);
  return i;
}
Example #2
0
int GaussSeidelPoisson1Drb(Vector u, double tol, int maxit)
{
  int it=0, i, j;
  double max = tol+1;
  double rl = maxNorm(u);
  Vector b = createVector(u->len);
  Vector r = createVector(u->len);
  Vector v = createVector(u->len);
  copyVector(b, u);
  fillVector(u, 0.0);
  while (max > tol && ++it < maxit) {
    copyVector(v, u);
    copyVector(u, b);
    for (j=0;j<2;++j) {
#pragma omp parallel for schedule(static)
      for (i=j;i<r->len;i+=2) {
        if (i > 0)
          u->data[i] += v->data[i-1];
        if (i < r->len-1)
          u->data[i] += v->data[i+1];
        r->data[i] = u->data[i]-(2.0+alpha)*v->data[i];
        u->data[i] /= (2.0+alpha);
        v->data[i] = u->data[i];
      }
    }
    max = maxNorm(r);
  }
  freeVector(b);
  freeVector(r);
  freeVector(v);

  return it;
}
Example #3
0
int main(int argc, char **argv)
{
    /* start timer */
    double clock;
    clock = timer();

    real **A, *x, *b;
    int n = 1000;

    A = allocMatrix(n, n);
    x = allocVector(n);
    b = allocVector(n);
    initializeA(A, n);
    initializeB(b, n);

    //showMatrix (n, A);
    //printf ("\n");

    solve(n, A, x, b);
    /* stop timer */
    clock = timer() - clock;

    showVector(n, x);

    freeMatrix(A);
    freeVector(x);
    freeVector(b);

    printf ("wallclock: %lf seconds\n", clock);

    return EXIT_SUCCESS;
}
Example #4
0
int dataExchange(unit_t tmin, unit_t tmax, pvector_t *v, pvector_t tmp) {
	index_t i, x, pos = 0;
	unit_t t;
	pvector_t data;

	if (initVector(&data, ELEMENTS_PER_PROCESS << 1) == NULL)
		return ERRORCODE_CANT_MALLOC;

	for(i = 0; i < PROCESS_NUMBER; i++) {
		if (ID == i) copyVector(*v, tmp, (*v)->length);

		if (MPI_Bcast(tmp->vector, listLength(i), MPI_UNIT, i, MPI_COMM_WORLD) != MPI_SUCCESS)
			return ERRORCODE_MPI_ERROR;

		for(x = 0; x < listLength(i); x++) {
			t = getFromVector(tmp, x);
			if (t <= tmax) {
				if (t > tmin)
					setInVector(data, pos++, t);
			} else
				x+= ELEMENTS_NUMBER;
		}
	}

	freeVector(v);
	if (initVector(v, pos) == NULL)
		return ERRORCODE_CANT_MALLOC;
	copyVector(data, *v, pos);
	freeVector(&data);
	return ERRORCODE_NOERRORS;
}
Example #5
0
int main(int argc, char **argv)
{
  real **A, *x, *b;

  A = allocMatrix(3, 3);
  x = allocVector(3);
  b = allocVector(3);

  A[0][0]= 0; A[0][1]= 2; A[0][2]=  3;
  A[1][0]= 3; A[1][1]= 0; A[1][2]=  1;
  A[2][0]= 6; A[2][1]= 2; A[2][2]=  8;

  b[0] = 5;
  b[1] = 4;
  b[2] = 16;
  
  showMatrix (3, A);
  printf ("\n");
  
  solve(3, A, x, b);
  
  showVector(3, x);
  
  freeMatrix(A);
  freeVector(x);
  freeVector(b);
  
  return EXIT_SUCCESS;
}
Example #6
0
int GaussJacobiPoisson1D(Vector u, double tol, int maxit)
{
  int it=0, i;
  double rl;
  double max = tol+1;
  Vector b = createVector(u->len);
  Vector e = createVector(u->len);
  copyVector(b, u);
  fillVector(u, 0.0);
  rl = maxNorm(b);
  while (max > tol*rl && ++it < maxit) {
    copyVector(e, u);
    copyVector(u, b);
#pragma omp parallel for schedule(static)
    for (i=0;i<e->len;++i) {
      if (i > 0)
        u->data[i] += e->data[i-1];
      if (i < e->len-1)
        u->data[i] += e->data[i+1];
      u->data[i] /= (2.0+alpha);
    }
    axpy(e, u, -1.0);
    max = maxNorm(e);
  }
  freeVector(b);
  freeVector(e);

  return it;
}
Example #7
0
int GaussJacobiPoisson1D(Vector u, double tol, int maxit)
{
  int it=0, i;
  Vector b = cloneVector(u);
  Vector e = cloneVector(u);
  copyVector(b, u);
  fillVector(u, 0.0);
  double max = tol+1;
  while (max > tol && ++it < maxit) {
    copyVector(e, u);
    collectVector(e);
    copyVector(u, b);
#pragma omp parallel for schedule(static)
    for (i=1;i<e->len-1;++i) {
      u->data[i] += e->data[i-1];
      u->data[i] += e->data[i+1];
      u->data[i] /= (2.0+alpha);
    }
    axpy(e, u, -1.0);
    e->data[0] = e->data[e->len-1] = 0.0;
    max = maxNorm(e);
  }
  freeVector(b);
  freeVector(e);

  return it;
}
Example #8
0
int GaussSeidelPoisson1D(Vector u, double tol, int maxit)
{
  int it=0, i, j;
  double max = tol+1;
  double rl = maxNorm(u);
  Vector b = createVector(u->len);
  Vector r = createVector(u->len);
  Vector v = createVector(u->len);
  copyVector(b, u);
  fillVector(u, 0.0);
  while (max > tol*rl && ++it < maxit) {
    copyVector(v, u);
    copyVector(u, b);
    for (i=0;i<r->len;++i) {
      if (i > 0)
        u->data[i] += v->data[i-1];
      if (i < r->len-1)
        u->data[i] += v->data[i+1];
      r->data[i] = u->data[i]-(2.0+alpha)*v->data[i];
      u->data[i] /= (2.0+alpha);
      v->data[i] = u->data[i];
    }
    max = maxNorm(r);
  }
  freeVector(b);
  freeVector(r);
  freeVector(v);

  return it;
}
Example #9
0
int main(int argc, char** argv)
{
  int rank, size;
  init_app(argc, argv, &rank, &size);

  if (argc < 2) {
    printf("usage: %s <N> [L]\n",argv[0]);
    close_app();
    return 1;
  }

  /* the total number of grid points in each spatial direction is (N+1) */
  /* the total number of degrees-of-freedom in each spatial direction is (N-1) */
  int N  = atoi(argv[1]);
  int M  = N-1;
  double L=1.0;
  if (argc > 2)
    L = atof(argv[2]);

  double h = L/N;

  Matrix A = createPoisson2D(M, 0.0);

  Vector grid = createVector(M);
  for (int i=0;i<M;++i)
    grid->data[i] = (i+1)*h;

  Vector u = createVector(M*M);
  evalMesh(u, grid, grid, poisson_source);
  scaleVector(u, h*h);

  double time = WallTime();
  llsolve(A, u);

  evalMesh2(u, grid, grid, exact_solution, -1.0);
  double max = maxNorm(u);

  if (rank == 0) {
    printf("elapsed: %f\n", WallTime()-time);
    printf("max: %f\n", max);
  }

  freeVector(u);
  freeVector(grid);
  freeMatrix(A);

  close_app();
  return 0;
}
Example #10
0
Vec3* Camera::vec3TransformCoord(Vec3* pOut, Vec3* pV, Mat4* pM)
{
	// Transforms the vector, pV (x, y, z, 1), by the matrix, pM,
	// projecting the result back into w=1
	*pOut = (freeVector(*pV) * (*pM)).project();
	return pOut;
}
Example #11
0
void DiagonalizationPoisson1Dfst(Vector u, const Vector lambda)
{
  Vector btilde = createVector(u->len);
  Vector buf = createVector(4*(u->len+1));
  int i;
  int N=u->len+1;
  int NN=4*N;
  copyVector(btilde, u);
  fst(btilde->data, &N, buf->data, &NN);
  for (i=0;i<btilde->len;++i)
    btilde->data[i] /= (lambda->data[i]+alpha);
  fstinv(btilde->data, &N, buf->data, &NN);
  copyVector(u, btilde);
  freeVector(btilde);
  freeVector(buf);
}
Example #12
0
int main(int argc,char* argv[]){

  vector* vec = createVector();
  printf("\nnew vector-pointer generated\n\n");

  writeVector(vec,argv[1]);

  printVector(vec);

  int searchResult = search(vec,10);
  printf("%d\n\n",searchResult);

  data* newData1 = createData(51,44,19.290122);
  addBegin(vec,newData1);
  printf("new data added to beginning\n\n");

  data* newData2 = createData(10,59,82.430034);
  addNpos(vec,newData2,6);
  printf("new data added to position 6\n\n");

  searchResult = search(vec,10);
  printf("%d\n\n",searchResult);

  printVector(vec);

  freeVector(vec);
  printf("vector freed\n");

  return 0;
}
Example #13
0
/** Frees the memory allocated for a matrix
 *
 */
void freeMatrix(double** M, size_t n)
{
  size_t i;
  for(i = 0; i < n; i++)
    freeVector(M[i]);

  free(M);
}
Example #14
0
void collectMatrix(Matrix u)
{
#ifdef HAVE_MPI
  int source, dest;
  // south
  MPI_Cart_shift(*u->as_vec->comm, 1, -1, &source, &dest);
  MPI_Sendrecv(u->data[1]+1, u->rows-2, MPI_DOUBLE, dest, 0,
               u->data[u->cols-1]+1, u->rows-2, MPI_DOUBLE, source, 0,
               *u->as_vec->comm, MPI_STATUS_IGNORE);

  // north
  MPI_Cart_shift(*u->as_vec->comm, 1, 1, &source, &dest);
  MPI_Sendrecv(u->data[u->cols-2]+1, u->rows-2, MPI_DOUBLE, dest, 1,
               u->data[0]+1, u->rows-2, MPI_DOUBLE, source, 1,
               *u->as_vec->comm, MPI_STATUS_IGNORE);

  Vector sendBuf = createVector(u->cols-2);
  Vector recvBuf = createVector(u->cols-2);

  // west
  MPI_Cart_shift(*u->as_vec->comm, 0, -1, &source, &dest);
  if (dest != MPI_PROC_NULL)
    copyVectorDispl(sendBuf, u->row[1], u->cols-2, 1);
  MPI_Sendrecv(sendBuf->data, sendBuf->len, MPI_DOUBLE, dest, 2,
               recvBuf->data, recvBuf->len, MPI_DOUBLE, source, 2,
               *u->as_vec->comm, MPI_STATUS_IGNORE);
  if (source != MPI_PROC_NULL)
    dcopy(&recvBuf->len, recvBuf->data, &recvBuf->stride,
          u->row[u->rows-1]->data+u->rows, &u->rows);

  // east
  MPI_Cart_shift(*u->as_vec->comm, 0, 1, &source, &dest);
  if (dest != MPI_PROC_NULL)
    copyVectorDispl(sendBuf, u->row[u->rows-2], u->cols-2, 1);
  MPI_Sendrecv(sendBuf->data, sendBuf->len, MPI_DOUBLE, dest, 2,
               recvBuf->data, recvBuf->len, MPI_DOUBLE, source, 2,
               *u->as_vec->comm, MPI_STATUS_IGNORE);
  if (source != MPI_PROC_NULL)
    dcopy(&recvBuf->len, recvBuf->data, &recvBuf->stride,
          u->row[0]->data+u->rows, &u->rows);

  freeVector(sendBuf);
  freeVector(recvBuf);
#endif
}
Example #15
0
double doSum(Vector vec){
  double sum=0;
  for (int i = 0; i < vec->len; ++i)
  {
    sum += vec->data[i];
  }
  freeVector(vec);
  return sum;
}
Example #16
0
void DiagonalizationPoisson1D(Vector u, const Vector lambda, const Matrix Q)
{
  Vector btilde = createVector(u->len);
  int i;
  MxV(btilde, Q, u, 1.0, 0.0, 'T');
  for (i=0;i<btilde->len;++i)
    btilde->data[i] /= lambda->data[i];
  MxV(u, Q, btilde, 1.0, 0.0, 'N');
  freeVector(btilde);
}
Example #17
0
void daxpyVector2(REAL *dense, REAL scalar, sparseVector *sparse, int indexStart, int indexEnd)
{
  sparseVector *hold;

  hold = createVector(sparse->limit, sparse->count);
  putDiagonalIndex(hold, getDiagonalIndex(sparse));
  putVector(hold, dense, indexStart, indexEnd);
  daxpyVector3(hold, scalar, sparse, indexStart, indexEnd);
  freeVector(hold);
}
Example #18
0
int main(int argc, char** argv)
{
	float *A=NULL, *B=NULL;
	long long k=0;
	struct timeval fin,ini;
	float sum=0;
	unsigned long long num;
	unsigned long thr;
	
	thr=atoi(argv[1]);
	num=atoi(argv[2]);

	omp_set_num_threads(thr);	

	A = generateVector(num);
	B = generateVector(num);
	if ( !A || !B )
	{
		printf("Error when allocationg matrix\n");
		freeVector(A);
		freeVector(B);
		return -1;
	}
	
	gettimeofday(&ini,NULL);
	/* Bloque de computo */
	sum = 0;
	#pragma omp parallel for
	for(k=0;k<num;k++)
	{
		sum = sum + A[k]*B[k];
	}
	/* Fin del computo */
	gettimeofday(&fin,NULL);

	printf("Resultado: %f\n",sum);
	printf("Tiempo: %f\n", ((fin.tv_sec*1000000+fin.tv_usec)-(ini.tv_sec*1000000+ini.tv_usec))*1.0/1000000.0);
	freeVector(A);
	freeVector(B);

	return 0;
}
Example #19
0
void Poisson1DPre(Vector u, Vector v)
{
  Vector tmp = collectBeforePre(v);
  if (A1D) {
    llsolve(A1D, tmp, A1Dfactored);
    A1Dfactored = 1;
  } else
    cgMatrixFree(Poisson1Dnoborder, tmp, 1e-10);
  collectAfterPre(u, tmp);
  freeVector(tmp);
}
Example #20
0
// calculates \sum_K v_i'*A*v_i
double dosum(Matrix A, Matrix v)
{
  double alpha=0;
  Vector temp = createVector(A->rows);
  for( int i=0;i<v->cols;++i ) {
    myMxV(temp,A,v->col[i]);
    alpha += myinnerproduct(v->col[i], temp);
  }
  freeVector(temp);

  return alpha;
}
Example #21
0
int main(int argc, char **argv)
{
    int num_threads, size, num_tries;
    float *A = NULL, *B = NULL;
    long long k = 0;
    struct timeval fin, ini;
    float sum = 0;

    parse_args(argc, argv, &num_threads, &size, &num_tries);
	omp_set_num_threads(num_threads);

    A = generateVector(size);
    B = generateVector(size);
    if ( !A || !B )
    {
        printf("Error when allocationg matrix\n");
        freeVector(A);
        freeVector(B);
        return -1;
    }

    gettimeofday(&ini, NULL);
    /* Bloque de computo */
    sum = 0;
    #pragma omp parallel for
    for (k = 0; k < size; k++)
    {
        sum = sum + A[k] * B[k];
    }
    /* Fin del computo */
    gettimeofday(&fin, NULL);

    printf("Resultado: %f\n", sum);
    printf("Tiempo: %f\n", ((fin.tv_sec * 1000000 + fin.tv_usec) - (ini.tv_sec * 1000000 + ini.tv_usec)) * 1.0 / 1000000.0);
    freeVector(A);
    freeVector(B);

    return 0;
}
Example #22
0
// perform a matrix-vector product
void myMxV(Vector u, Matrix A, Vector v)
{
  Vector temp = createVector(A->rows);
  MxV(temp, A, v);
#ifdef HAVE_MPI
  for (int i=0;i<v->comm_size;++i)
    MPI_Reduce(temp->data+v->displ[i], u->data, v->sizes[i],
               MPI_DOUBLE, MPI_SUM, i, *v->comm);
#else
  memcpy(u->data, temp->data, u->len*sizeof(double));
#endif
  freeVector(temp);
}
Example #23
0
void parallelPoisson(int problemSize,MPI_Comm comm)
{
	int numberOfColumns = problemSize-1;//numberOfUnknowns
	int fstBufferSize = 4*problemSize;
	double stepSize = 1.0/problemSize;

	double startTime, endTime;
	Vector diagonal = createVector(numberOfColumns);
	Vector fstBuffer = createVector(fstBufferSize);

	ColumnMatrix local_b = createColumnMatrixMPI(numberOfColumns,&comm);
	ColumnMatrix local_bt = createColumnMatrixMPI(numberOfColumns,&comm);
	ColumnMatrix sendBuffer = createColumnMatrixMPI(numberOfColumns,&comm);
	ColumnMatrix recvBuffer = createColumnMatrixMPI(numberOfColumns,&comm);

//	MPI_Datatype columnSendType; 
//	createMPIColumnSendType(local_b, &columnSendType);
	startTime = WallTime();
	diagonalEigenvalues(diagonal);

	initRightHandSide(local_b,stepSize);

	fastSineTransform(local_b,fstBuffer);

	mpiColumnMatrixTranspose(local_bt, recvBuffer, local_b, sendBuffer);

	fastSineTransformInv(local_bt,fstBuffer);
	
	systemSolver(local_bt,diagonal);

	fastSineTransform(local_bt,fstBuffer);
	
	mpiColumnMatrixTranspose(local_b, recvBuffer,local_bt,sendBuffer);
	
	fastSineTransformInv(local_b,fstBuffer);



	findAndPrintUmax(local_b);
	endTime = WallTime();
	if(local_b->commRank ==0)
		printf("Runtime: %fs\n",endTime-startTime);
	freeVectorMPI(diagonal);
	freeColumnMatrixMPI(local_b);
	freeColumnMatrixMPI(local_bt);
	freeVector(fstBuffer);
	freeColumnMatrixMPI(sendBuffer);
	freeColumnMatrixMPI(recvBuffer);
	//MPI_Type_free(&columnSendType);		

}
Example #24
0
API void freeOpenGLLodMap(OpenGLLodMap *lodmap)
{
	g_hash_table_remove(maps, lodmap->quadtree);

	g_thread_pool_free(lodmap->loadingPool, false, true);
	freeQuadtree(lodmap->quadtree);

	deleteOpenGLMaterial("lodmap");
	freeOpenGLPrimitive(lodmap->heightmap);
	freeVector(lodmap->viewerPosition);
	freeOpenGLLodMapDataSource(lodmap->source);

	free(lodmap);
}
Example #25
0
int GaussSeidel(Matrix A, Vector u, int maxit)
{
  int it=0, i, j;
  Vector b = createVector(u->len);
  Vector v = createVector(u->len);
  copyVector(b, u);
  fillVector(u, 0.0);
  while (++it < maxit) {
    copyVector(v, u);
    copyVector(u, b);
    for (i=0;i<A->rows;++i) {
      for (j=0;j<A->cols;++j) {
        if (j != i)
          u->data[i] -= A->data[j][i]*v->data[j];
      }
      u->data[i] /= A->data[i][i];
      v->data[i] = u->data[i];
    }
  }
  freeVector(b);
  freeVector(v);

  return it;
}
Example #26
0
// calculates \sum_K v_i'*A*v_i
double dosum(Matrix A, Matrix v)
{
  double alpha=0;
  Vector temp = createVector(A->rows);
  for( int i=0;i<v->cols;++i ) {
    myMxV(temp,A,v->col[i]);
    alpha += myinnerproduct(temp,v->col[i]);
  }
  freeVector(temp);
#ifdef HAVE_MPI
  double s2=alpha;
  MPI_Allreduce(&s2, &alpha, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
#endif

  return alpha;
}
Example #27
0
double doSum(Vector vec){
  double sum=0;
  double one=1.;
  //Generating a vector of one's, to take advantace of blas-ddot?
  Vector oneVec = createPointerVector(vec->len);
  for (int i = 0; i < vec->len; ++i)
  {
    oneVec->data[i]=*&one;
  }
  for (int i = 0; i < vec->len; ++i)
  {
    sum = innerproduct(vec, oneVec);
  }
  freeVector(vec);
  return sum;
}
Example #28
0
void MxV(Vector y, const Matrix A, const Vector x, double alpha, double beta)
{
  char trans='N';
  int one=1;
#ifdef HAVE_MPI
  Vector temp = createVector(A->rows);
  copyVector(y, x);
#else
  Vector temp=y;
#endif
  dgemv(&trans, &A->rows, &A->cols, &alpha, A->data[0], &A->rows, x->data,
        &x->stride, &beta, temp->data, &one);
#ifdef HAVE_MPI
  for (int i=0;i<x->comm_size;++i) {
    MPI_Reduce(temp->data+x->displ[i], y->data, x->sizes[i],
               MPI_DOUBLE, MPI_SUM, i, *x->comm);
  }
  freeVector(temp);
#endif
}
Example #29
0
void resizeMatrix(sparseMatrix *matrix, int newSize)
{
  int oldSize;

  if(matrix == NULL)
    oldSize = 0;
  else
    oldSize = matrix->size;
  while(oldSize>newSize) {
	  oldSize--;
	  freeVector(matrix->list[oldSize]);
    return;
  }
  REALLOC(matrix->list, newSize);
  while(oldSize<newSize) {
	  matrix->list[oldSize] = NULL;
	  oldSize++;
  }
  if(newSize>0)
    matrix->size = newSize;
}
Example #30
0
/**
 * Deactivates an active LOD map tile
 *
 * @param tile			the LOD map tile to deactivate
 */
static void deactivateLodMapTile(OpenGLLodMapTile *tile)
{
	assert(tile->status == OPENGL_LODMAP_TILE_ACTIVE);

	freeOpenGLModel(tile->model);
	tile->model = NULL;

	freeOpenGLTexture(tile->heightsTexture);
	tile->heightsTexture = NULL;

	freeOpenGLTexture(tile->normalsTexture);
	tile->normalsTexture = NULL;

	freeOpenGLTexture(tile->textureTexture);
	tile->textureTexture = NULL;

	freeVector(tile->parentOffset);
	tile->parentOffset = NULL;

	tile->status = OPENGL_LODMAP_TILE_READY;
}