model-layer/scalable-tf-module/FTcom/Tensor.cpp
/* Build a Tensor object from the raw text file */
Build a Tensor object from the raw text file
Build a Tensor object from the raw text file
void Tensor::buildTensorFromRawFile(const char *InputPath) { 
	FILE *fp;
	int n, i, j, idx;
	double v;
	char tmp[1005];
	order = Option::tensorOrder;
	Dims = (int *)malloc(sizeof(int)*order);
	memset(Dims, 0, sizeof(int)*order);
	fp = fopen(InputPath, "r");
	while (fgets(tmp, 1005, fp)) nnz++;
	fclose(fp);
	IndexMat = (int **)malloc(sizeof(int*)*nnz);
	IndexMat[0] = (int *)malloc(sizeof(int)*nnz*order);
	for(n = 0; n < nnz; n++) IndexMat[n] = &IndexMat[0][n*order];
	val = (double *)malloc(sizeof(double)*nnz);
	fp = fopen(InputPath, "r");
	for (n = 0; n < nnz; n++) {
		for (i = 0; i < order; i++) {
			fscanf(fp, "%d", &idx);
			IndexMat[n][i] = idx - 1;
			if (Dims[i] < idx) Dims[i] = idx;
		}
		fscanf(fp, "%lf", &v);
		val[n] = v;
	}
	fclose(fp);
	int cntSize = 0;
	for (i = 0; i < order; i++)
		cntSize += Dims[i] + 1;
	WhereMat = (int **)malloc(sizeof(int*)*order);
	WhereMat[0] = (int *)malloc(sizeof(int)*order*nnz);
	for(i = 0; i < order; i++) WhereMat[i] = &WhereMat[0][i*nnz];
	CountMat = (int **)malloc(sizeof(int*)*order);
	CountMat[0] = (int *)malloc(sizeof(int)*cntSize);
	for(i = 1; i < order; i++) CountMat[i] = CountMat[i-1] + Dims[i-1] + 1;
	assignTensorIndex();
}
void Tensor :: buildTensorFromRawFile ( const char * InputPath ) { FILE * fp ; int n , i , j , idx ; double v ; char tmp [ 1005 ] ; order = Option :: tensorOrder ; Dims = ( int * ) malloc ( sizeof ( int ) * order ) ; memset ( Dims , 0 , sizeof ( int ) * order ) ; fp = fopen ( InputPath , " " ) ; while ( fgets ( tmp , 1005 , fp ) ) nnz ++ ; fclose ( fp ) ; IndexMat = ( int * * ) malloc ( sizeof ( int * ) * nnz ) ; IndexMat [ 0 ] = ( int * ) malloc ( sizeof ( int ) * nnz * order ) ; for ( n = 0 ; n < nnz ; n ++ ) IndexMat [ n ] = & IndexMat [ 0 ] [ n * order ] ; val = ( double * ) malloc ( sizeof ( double ) * nnz ) ; fp = fopen ( InputPath , " " ) ; for ( n = 0 ; n < nnz ; n ++ ) { for ( i = 0 ; i < order ; i ++ ) { fscanf ( fp , " " , & idx ) ; IndexMat [ n ] [ i ] = idx - 1 ; if ( Dims [ i ] < idx ) Dims [ i ] = idx ; } fscanf ( fp , " " , & v ) ; val [ n ] = v ; } fclose ( fp ) ; int cntSize = 0 ; for ( i = 0 ; i < order ; i ++ ) cntSize += Dims [ i ] + 1 ; WhereMat = ( int * * ) malloc ( sizeof ( int * ) * order ) ; WhereMat [ 0 ] = ( int * ) malloc ( sizeof ( int ) * order * nnz ) ; for ( i = 0 ; i < order ; i ++ ) WhereMat [ i ] = & WhereMat [ 0 ] [ i * nnz ] ; CountMat = ( int * * ) malloc ( sizeof ( int * ) * order ) ; CountMat [ 0 ] = ( int * ) malloc ( sizeof ( int ) * cntSize ) ; for ( i = 1 ; i < order ; i ++ ) CountMat [ i ] = CountMat [ i - 1 ] + Dims [ i - 1 ] + 1 ; assignTensorIndex ( ) ; }
Build a Tensor object from the raw text file
Build a Tensor object from the raw text file