model-layer/scalable-tf-module/FTcom/util.cpp
/* 1) Initialize all factor matrices and a core tensor, and 2) Build a test tensor if there exists a test input */
1) Initialize all factor matrices and a core tensor, and 2) Build a test tensor if there exists a test input
1 ) Initialize all factor matrices and a core tensor and 2 ) Build a test tensor if there exists a test input
void Initialize(double ***&FactorMat, Tensor &G, int *&Dims, Tensor &Xtest) {
	printf("Initializing all factor matrices and a core tensor ... ");
	int n, i, j, k, g; 
	int order = Option::tensorOrder;
	int rankSize = Option::rankSize;
	int gridSize = Option::gridSize;
	char *InputPath = Option::trainPath;
	Dims = (int *)malloc(sizeof(int)*order);
	int **gridDimCnt, **gridDims;
	Read_Grid_Info(InputPath, gridDimCnt, gridDims);
	for (i = 0; i < order; i++) Dims[i] = gridDimCnt[i][gridSize]; 
	free(gridDimCnt[0]); free(gridDimCnt);
	free(gridDims[0]); free(gridDims);
	G.buildCoreTensor(rankSize);
	int cntDims = 0, totalDims = 0;
	for (i = 0; i < order; i++) totalDims += Dims[i];
	FactorMat = (double ***)malloc(sizeof(double **)*order);
	FactorMat[0] = (double **)malloc(sizeof(double *)*totalDims);
	FactorMat[0][0] = (double *)malloc(sizeof(double)*totalDims*rankSize);
	for (i = 0; i < order; i++) {
		if (i >= 1) {
			FactorMat[i] = FactorMat[i-1] + Dims[i-1];
			cntDims += Dims[i-1];
		}
		int row = Dims[i], col = G.Dims[i];
		for (j = 0; j < row; j++) {
			FactorMat[i][j] = &FactorMat[0][0][(cntDims + j)*col];
			for (k = 0; k < col; k++) {
				if (i == 0) FactorMat[i][j][k] = 0;
				else FactorMat[i][j][k] = frand(0, 1);
			}
		}
	}
	if(Option::testPath != NULL) {
		FILE *fp;
		char tmp[1005];
		int idx; double val;	
		fp = fopen(Option::testPath, "r");
		while (fgets(tmp, 1005, fp)) Xtest.nnz++;
		fclose(fp);
		Xtest.val = (double *)malloc(sizeof(double)*Xtest.nnz);
		Xtest.IndexMat = (int **)malloc(sizeof(int *)*Xtest.nnz);
		Xtest.IndexMat[0] = (int *)malloc(sizeof(int)*Xtest.nnz*order);
		for (n = 0; n < Xtest.nnz; n++) Xtest.IndexMat[n] = &(Xtest.IndexMat[0][n*order]);
		fp = fopen(Option::testPath, "r");
		for (n = 0; n < Xtest.nnz; n++) {
			for (i = 0; i < order; i++) {
				fscanf(fp, "%d", &idx);
				Xtest.IndexMat[n][i] = idx-1;
			}
			fscanf(fp, "%lf", &val);
			Xtest.val[n] = val;
		}
		fclose(fp);
	}
	printf("Done ");
	printf("=> Dimensionalities: %d ", Dims[0]);
	for (i = 1; i < order; i++) printf("x %d ", Dims[i]);
	printf("\n\n");
}
void Initialize ( double * * * & FactorMat , Tensor & G , int * & Dims , Tensor & Xtest ) { printf ( " " ) ; int n , i , j , k , g ; int order = Option :: tensorOrder ; int rankSize = Option :: rankSize ; int gridSize = Option :: gridSize ; char * InputPath = Option :: trainPath ; Dims = ( int * ) malloc ( sizeof ( int ) * order ) ; int * * gridDimCnt , * * gridDims ; Read_Grid_Info ( InputPath , gridDimCnt , gridDims ) ; for ( i = 0 ; i < order ; i ++ ) Dims [ i ] = gridDimCnt [ i ] [ gridSize ] ; free ( gridDimCnt [ 0 ] ) ; free ( gridDimCnt ) ; free ( gridDims [ 0 ] ) ; free ( gridDims ) ; G . buildCoreTensor ( rankSize ) ; int cntDims = 0 , totalDims = 0 ; for ( i = 0 ; i < order ; i ++ ) totalDims += Dims [ i ] ; FactorMat = ( double * * * ) malloc ( sizeof ( double * * ) * order ) ; FactorMat [ 0 ] = ( double * * ) malloc ( sizeof ( double * ) * totalDims ) ; FactorMat [ 0 ] [ 0 ] = ( double * ) malloc ( sizeof ( double ) * totalDims * rankSize ) ; for ( i = 0 ; i < order ; i ++ ) { if ( i >= 1 ) { FactorMat [ i ] = FactorMat [ i - 1 ] + Dims [ i - 1 ] ; cntDims += Dims [ i - 1 ] ; } int row = Dims [ i ] , col = G . Dims [ i ] ; for ( j = 0 ; j < row ; j ++ ) { FactorMat [ i ] [ j ] = & FactorMat [ 0 ] [ 0 ] [ ( cntDims + j ) * col ] ; for ( k = 0 ; k < col ; k ++ ) { if ( i == 0 ) FactorMat [ i ] [ j ] [ k ] = 0 ; else FactorMat [ i ] [ j ] [ k ] = frand ( 0 , 1 ) ; } } } if ( Option :: testPath != NULL ) { FILE * fp ; char tmp [ 1005 ] ; int idx ; double val ; fp = fopen ( Option :: testPath , " " ) ; while ( fgets ( tmp , 1005 , fp ) ) Xtest . nnz ++ ; fclose ( fp ) ; Xtest . val = ( double * ) malloc ( sizeof ( double ) * Xtest . nnz ) ; Xtest . IndexMat = ( int * * ) malloc ( sizeof ( int * ) * Xtest . nnz ) ; Xtest . IndexMat [ 0 ] = ( int * ) malloc ( sizeof ( int ) * Xtest . nnz * order ) ; for ( n = 0 ; n < Xtest . nnz ; n ++ ) Xtest . IndexMat [ n ] = & ( Xtest . IndexMat [ 0 ] [ n * order ] ) ; fp = fopen ( Option :: testPath , " " ) ; for ( n = 0 ; n < Xtest . nnz ; n ++ ) { for ( i = 0 ; i < order ; i ++ ) { fscanf ( fp , " " , & idx ) ; Xtest . IndexMat [ n ] [ i ] = idx - 1 ; } fscanf ( fp , " " , & val ) ; Xtest . val [ n ] = val ; } fclose ( fp ) ; } printf ( " " ) ; printf ( " " , Dims [ 0 ] ) ; for ( i = 1 ; i < order ; i ++ ) printf ( " " , Dims [ i ] ) ; printf ( " \n \n " ) ; }
1) Initialize all factor matrices and a core tensor, and 2) Build a test tensor if there exists a test input
1 ) Initialize all factor matrices and a core tensor and 2 ) Build a test tensor if there exists a test input