model-layer/scalable-tf-module/FTcom/Tensor.cpp
/* Read the subtensor (i.e., X_g[g_1, g_2, ..., g_N]) from a grid-based tensor file */
Read the subtensor  from a grid-based tensor file
Read the subtensor from a grid - based tensor file
void SubTensor::readSubTensorFromGTF(const int fd, int cellIdx, int *gridIdx, int **gridDims) {
	int gridSize, order, cntSize = 0;
	off_t gridOffset;
	off_t readSize;
	pread(fd, &gridSize, sizeof(int), 0);
	pread(fd, &order, sizeof(int), sizeof(int));
	pread(fd, &gridOffset, sizeof(off_t), sizeof(int)*2 + sizeof(int)*order*(gridSize+1) + sizeof(off_t)*cellIdx);
	for (int i = 0; i < order; i++)
		cntSize += gridDims[i][gridIdx[i]] + 1;
	lseek(fd, gridOffset, SEEK_SET);
	read(fd, &nnz, sizeof(int));
	ptr = (int *)malloc(sizeof(int)*(nnz*order*2 + cntSize));
	val = (double *)malloc(sizeof(double)*nnz);	
	IndexPtr = ptr;
	WherePtr = IndexPtr + nnz*order;
	CountPtr = WherePtr + nnz*order;
	IndexMat = (int **)malloc(sizeof(int *)*nnz);
	WhereMat = (int **)malloc(sizeof(int *)*order);
	CountMat = (int **)malloc(sizeof(int *)*order);
	for (int i = 0; i < nnz; i++) IndexMat[i] = &IndexPtr[i*order];
	for (int i = 0; i < order; i++) WhereMat[i] = &WherePtr[i*nnz];
	for (int i = 0; i < order; i++) {
		if (i == 0) CountMat[i] = CountPtr;
		else CountMat[i] = CountMat[i-1] + gridDims[i-1][gridIdx[i-1]] + 1;
	}
	read(fd, ptr, sizeof(int)*(nnz*order*2 + cntSize));
	read(fd, val, sizeof(double)*nnz);
}
void SubTensor :: readSubTensorFromGTF ( const int fd , int cellIdx , int * gridIdx , int * * gridDims ) { int gridSize , order , cntSize = 0 ; off_t gridOffset ; off_t readSize ; pread ( fd , & gridSize , sizeof ( int ) , 0 ) ; pread ( fd , & order , sizeof ( int ) , sizeof ( int ) ) ; pread ( fd , & gridOffset , sizeof ( off_t ) , sizeof ( int ) * 2 + sizeof ( int ) * order * ( gridSize + 1 ) + sizeof ( off_t ) * cellIdx ) ; for ( int i = 0 ; i < order ; i ++ ) cntSize += gridDims [ i ] [ gridIdx [ i ] ] + 1 ; lseek ( fd , gridOffset , SEEK_SET ) ; read ( fd , & nnz , sizeof ( int ) ) ; ptr = ( int * ) malloc ( sizeof ( int ) * ( nnz * order * 2 + cntSize ) ) ; val = ( double * ) malloc ( sizeof ( double ) * nnz ) ; IndexPtr = ptr ; WherePtr = IndexPtr + nnz * order ; CountPtr = WherePtr + nnz * order ; IndexMat = ( int * * ) malloc ( sizeof ( int * ) * nnz ) ; WhereMat = ( int * * ) malloc ( sizeof ( int * ) * order ) ; CountMat = ( int * * ) malloc ( sizeof ( int * ) * order ) ; for ( int i = 0 ; i < nnz ; i ++ ) IndexMat [ i ] = & IndexPtr [ i * order ] ; for ( int i = 0 ; i < order ; i ++ ) WhereMat [ i ] = & WherePtr [ i * nnz ] ; for ( int i = 0 ; i < order ; i ++ ) { if ( i == 0 ) CountMat [ i ] = CountPtr ; else CountMat [ i ] = CountMat [ i - 1 ] + gridDims [ i - 1 ] [ gridIdx [ i - 1 ] ] + 1 ; } read ( fd , ptr , sizeof ( int ) * ( nnz * order * 2 + cntSize ) ) ; read ( fd , val , sizeof ( double ) * nnz ) ; }
Read the subtensor (i.e., X_g[g_1, g_2, ..., g_N]) from a grid-based tensor file
Read the subtensor ( i . e . X_g [ g_1 g_2 ... g_N ] ) from a grid - based tensor file