Exemplo n.º 1
0
/* calc observation matrix Q for given observer*/
void
findQ(void)
{
	vec1	alpha, beta, gamma, v, w;
	double	E[5][5];
	double	F[5][5];
	double	G[5][5];
	double	H[5][5];
	double	U[5][5];

	/* calc translation matrix F*/
	tran3(eye.x, eye.y, eye.z, F);

	/* calc rotation matrix G*/
	alpha = angle(-direct.x, -direct.y);
	rot3(3, alpha, G);

	/* calc rotation matrix H*/
	v = sqrt(direct.x*direct.x + direct.y*direct.y);
	beta = angle(-direct.z, v);
	rot3(2, beta, H);

	/* calc rotation matrix U*/
	w = sqrt(v*v + direct.z*direct.z);
	gamma = angle(-direct.x*w, direct.y*direct.z);
	rot3(3, -gamma, U);

	/* combine the transformations to find Q*/
	mult3(G, F, Q);
	mult3(H, Q, E);
	mult3(U, E, Q);
}
Exemplo n.º 2
0
void recebe ()
{
	int num,i,cont=0;
	mult3(&num, &i, &cont);

	return;
}
Exemplo n.º 3
0
/* draw mathematical function plotfn*/
void
drawgrid(vec1 xmin, vec1 xmax, int nx, vec1 zmin, vec1 zmax, int nz)
{
	int	i, j;
	vec1	xi, xstep, yij;
	vec1	zj, zstep;
	vec2	v[2][100];
	double	S[5][5];

	/* scale it down*/
	scale3(1.0/(xmax-xmin)*2, 1.0/(xmax-xmin)*2, 1.0/(zmax-zmin), S);
	mult3(Q, S, Q);

	/* grid from xmin to xmax in nx steps and zmin to xmax in nz steps*/
	xstep = (xmax-xmin)/nx;
	zstep = (zmax-zmin)/nz;
	xi = xmin;
	zj = zmin;

	/* calc grid points on first fixed-z line, fine the y-height
	 * and transfrorm the points (xi,yij,zj) into observed
	 * position.  Observed first set stored in v[0,1..nx]
	 */
	for(i=0; i<=nx; ++i) {
		yij = plotfn(xi, zj);
		v[0][i].x = Q[1][1]*xi + Q[1][2]*yij + Q[1][3]*zj;
		v[0][i].y = Q[2][1]*xi + Q[2][2]*yij + Q[2][3]*zj;
		xi += xstep;
	}

	/* run thru consecutive fixed-z lines (the second set)*/
	for(j=0; j<nz; ++j) {
		xi = xmin;
		zj += zstep;

		/* calc grid points on this second set, find the
		 * y-height and transform the points (xi,yij,zj)
		 * into observed position.  Observed second set
		 * stored in v[1,0..nx]
		 */
		for(i=0; i<=nx; ++i) {
			yij = plotfn(xi, zj);
			v[1][i].x = Q[1][1]*xi + Q[1][2]*yij + Q[1][3]*zj;
			v[1][i].y = Q[2][1]*xi + Q[2][2]*yij + Q[2][3]*zj;
			xi += xstep;
		}

		/* run thru the nx patches formed by these two sets*/
		for(i=0; i<nx; ++i)
			patch(v[0][i], v[0][i+1], v[1][i], v[1][i+1]);

		/* copy second set into first set*/
		for(i=0; i<=nx; ++i)
			v[0][i] = v[1][i];
	}
}
Exemplo n.º 4
0
// Rounding error avoidance
inline void extendTo(vec3 * r, vec3 d) {
// 	if( d.x > 0 ) {
// 		r->x = nextScalar( r->x );
// 	}
// 	else {
// 		r->x = prevScalar( r->x );
// 	}
// 	if( d.y > 0 ) {
// 		r->y = nextScalar( r->y );
// 	}
// 	else {
// 		r->y = prevScalar( r->y );
// 	}
// 	if( d.z > 0 ) {
// 		r->z = nextScalar( r->z );
// 	}
// 	else {
// 		r->z = prevScalar( r->z );
// 	}
	vec3 ext;
	mult3( &ext, d, 0.1 );
	addTo3( r, ext );
}
Exemplo n.º 5
0
/* Main computational kernel. The whole function will be timed,
   including the call and return. */
    static
void kernel_floyd_warshall(int n,
        DATA_TYPE POLYBENCH_2D(path,N,N,n,n))
{
    int i1, i2, j3;

#pragma scop

    for(int k=0; k< _PB_N; k++){
        multifor(i1=0,i2=0,j3=0; i1<k+1, i2<k+1, j3<k+1; i1++,i2++,j3++; 1,1,1; 0,1,1){
0:{
      mult1(i1, path, k);
  }
1:{
      mult2(i2,path, n, k);
  }
2:{
      mult3(j3,path,n,k);
  }
        }
    for(int i=k+1; i<n;i++){
        for(int j=k+1;j<n;j++){
            path[i][j] = path[i][j] < path[i][k] + path[k][j] ? path[i][j] : path[i][k] + path[k][j];
        }
    }
    }
    //source code  for (k = 0; k < _PB_N; k++)
    //source code  {
    //source code      for(i = 0; i < _PB_N; i++)
    //source code          for (j = 0; j < _PB_N; j++)
    //source code              path[i][j] = path[i][j] < path[i][k] + path[k][j] ?
    //source code                  path[i][j] : path[i][k] + path[k][j];
    //source code  }
#pragma endscop

}
int main(void)
{
	FILE *finalB1L1 = fopen("finalB1L1.txt", "r");
	FILE *finalB1L2 = fopen("finalB1L2.txt", "r");
	FILE *finalW1L1 = fopen("finalW1L1.txt", "r");
	FILE *finalW1L2 = fopen("finalW1L2.txt", "r");
	FILE *finalSoftmaxTheta = fopen("finalSoftmaxTheta.txt", "r");
	FILE *testData = fopen("testData.txt", "r");
	FILE *testLabels = fopen("testLabels.txt", "r");
	FILE *testresult = fopen("result.txt", "r");

	int Labels[10000];

	char line[8000];
	char *ptr;
	int i;
	int j;


	void *VA;
	int fd;
	clock_t time;


	if ((fd = open("/dev/mem", (O_RDWR|O_SYNC))) == -1) {
		perror("ERROR: could not open \"/dev/mem\"\n");
		return 1;
	}

	VA = mmap(NULL, HW_SPAN, (PROT_READ|PROT_WRITE), MAP_SHARED, fd, HW_BASE);
	if (VA == MAP_FAILED) {
		perror("ERROR: mmap() failed ... \n");
		close(fd);
		return 1;
	}
	sdram = VA + ((unsigned long)(HW_BASE + 0x00) & (unsigned long)(HW_MASK));
	printf("sdram = 0x%X\n", (unsigned int)sdram);





	puts("parsing start");
	time = (float) clock();
	i = 0;
	while (fgets(line, 8000, finalB1L1) != NULL) {
		line[strlen(line)-1] = '\0';
		((float*)sdram)[B1L1_OFFSET+i] = atof(line);
		i++;
	}


	i = 0;
	while (fgets(line, 8000, finalB1L2) != NULL) {
		line[strlen(line)-1] = '\0';
		((float*)sdram)[B1L2_OFFSET+i] = atof(line);
		i++;
	}

	i = 0;
	while (fgets(line, 8000, testLabels) != NULL) {
		line[strlen(line)-1] = '\0';
		Labels[i] = atoi(line);
		i++;
	}


	unsigned short buf = 0;
	int count = 0;
	int writeCount = 0;

	while (fgets(line, 8000, finalW1L1) != NULL) {
		line[strlen(line)-1] = '\0';
		ptr = strtok(line, ",");

		while (ptr != NULL) {
			count++;
			buf <<= 4;
			buf = buf | ((short)(round(atof(ptr))) & 0xF);

			if (count == BUS_WITH/4) {
				count = 0;
				((short*)sdram)[WEIGHTS1_OFFSET + writeCount++] = buf;
				buf = 0;
			}
			ptr = strtok(NULL, ",");
		}
	}


	buf = 0;
	count = 0;
	writeCount = 0;

	while (fgets(line, 8000, finalW1L2) != NULL) {
		line[strlen(line)-1] = '\0';
		ptr = strtok(line, ",");

		while (ptr != NULL) {
			count++;
			buf <<= 4;
			buf = buf | ((short)(round(atof(ptr))) & 0xF);

			if (count == BUS_WITH/4) {
				count = 0;
				((short*)sdram)[WEIGHTS2_OFFSET + writeCount++] = buf;
				buf = 0;
			}
			ptr = strtok(NULL, ",");
		}
	}


	rewind(finalW1L2);
	i = 0;
	j = 0;
	while (fgets(line, 8000, finalW1L2) != NULL) {
		line[strlen(line)-1] = '\0';
		ptr = strtok(line, ",");

		while (ptr != NULL) {
			((float*)sdram)[W1L2_OFFSET+i*200+j] = atof(ptr);
			j++;
			ptr = strtok(NULL, ",");
		}
		j = 0;
		i++;
	}


	i = 0;
	j = 0;
	while (fgets(line, 8000, finalSoftmaxTheta) != NULL) {
		line[strlen(line)-1] = '\0';
		ptr = strtok(line, ",");

		while (ptr != NULL) {
			((float*)sdram)[SOFTMAX_OFFSET+i*200+j] = atof(ptr);
			j++;
			ptr = strtok(NULL, ",");
		}
		j = 0;
		i++;
	}

	i = 0;
	j = 0;
	while (fgets(line, 8000, testData) != NULL) {
		line[strlen(line)-1] = '\0';
		ptr = strtok(line, ",");

		while (ptr != NULL) {
			((float*)sdram)[DATA_OFFSET+i*784+j] = round(atof(ptr));
			j++;
			ptr = strtok(NULL, ",");
		}
		j = 0;
		i++;
	}

	fprintf(stderr, "parsing end: %.2f seconds\n",(float) (clock() - time) / CLOCKS_PER_SEC);






	float max = 0;
	int maxidx = 0;
	int fignum = 0;
	count = 0;

	time = (float) clock();
	for (fignum = 0; fignum < FIGNUM; fignum++) {
		//writeZero();
		mult1(fignum, 200, 784, 1);
		//writeResult(results, fignum);
		add(HIDDEN1_OFFSET, B1L1_OFFSET, 200, 1);
		sigmoid(HIDDEN1_OFFSET, 200);

		//mult2(W1L2_OFFSET, HIDDEN1_OFFSET, 200, 200, 1);
		newmult2(W1L2_OFFSET, HIDDEN1_OFFSET, 200, 200, 1);
		add(HIDDEN2_OFFSET, B1L2_OFFSET, 200, 1);
		sigmoid(HIDDEN2_OFFSET, 200);

		mult3(SOFTMAX_OFFSET, HIDDEN2_OFFSET, 10, 200, 1);
		sigmoid(OUT_OFFSET, 10);
		
		for (i = 0; i< 10; i++ ) {
			//printf("%.3f\t", final[i][0]);
			if (((float*)sdram)[OUT_OFFSET+i] > max) {
				max = ((float*)sdram)[OUT_OFFSET+i];
				maxidx = i + 1;
			}
		}
		printf("max_index = %d; expected %d\n", maxidx, Labels[fignum]);

		if (Labels[fignum] == maxidx) {
			count++;
		}
		
		max = 0;
		maxidx = 0;
	}

	fprintf(stderr, "Calculation: %.2f seconds\n",(float) (clock() - time) / CLOCKS_PER_SEC);
	printf("sample size = %d, accuracy = %f\n", fignum, count / (float)fignum);
	return 0;
}
Exemplo n.º 7
0
inline void reflect3(vec3 * r, vec3 n) {
	vec3 i = *r;	
	mult3( r, n, dot3( n, i ) * ((scalar)2) );
	subFrom3( r, i );
}
Exemplo n.º 8
0
inline void projectOnto3(vec3 a, vec3 * r) {
	scalar l = length3(a) * length3(*r);
	mult3( r, a, dot3(a, *r) );
	invScale3( r, l );
}
Exemplo n.º 9
0
inline void projectedOnto3(vec3 * r, vec3 a, vec3 b) {
	mult3( r, a, dot3(a, b) );
	invScale3( r, length3(a) * length3(b) );
}