dgemm_prefetch.c 9.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#include <assert.h>
#include <cblas.h>
#include <errno.h>
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>


#define ITER 10
#define MEMSIZES 134217728//1024 entries by 1024 entries * sizeof(unsigned long)
#define NUMBER_OF_THREADS 32
#define L2_CACHE_SIZE 1048576 //1MB
#define HBM_SIZE 17179869184 //16 GB

#define VERBOSE 0 //Verbose mode will print out extra information about what is happening
#define DEBUG 0 //This will print out verbose messages and debugging statements
#define PRINT_ARRAYS 0 
#define BILLION 1000000000L

#include <aml.h>

AML_TILING_2D_DECL(tiling);
AML_TILING_2D_DECL(tilingB);
AML_AREA_LINUX_DECL(slow);
AML_AREA_LINUX_DECL(fast);
AML_SCRATCH_PAR_DECL(sa);
AML_SCRATCH_PAR_DECL(sb);
AML_SCRATCH_PAR_DECL(sc);

size_t numthreads;
//size of 2D Tiles in A matrix
size_t tilesz, esz;
size_t numTiles;
unsigned long CHUNKING;
double *a, *b, *c;
unsigned long MEMSIZE;
unsigned long esize, numRows, rowLengthInBytes;
unsigned long rowSizeOfTile, rowSizeInTiles;
unsigned long long beginTime, endTime;
struct timespec startClock, endClock;
double elapsedTime;



//This code will take cycles executed as a use for timing the kernel.

unsigned long rdtsc(){
    unsigned int lo,hi;
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return ((unsigned long)hi << 32) | lo;
}



void do_work()
{
	int i, k, ai, bi, oldai, oldbi, tilesPerCol;
	int lda = (int)rowSizeOfTile, ldb, ldc;
	ldb = lda;
	ldc = lda;
	double *ap, *bp, *cp;
	void *abaseptr, *bbaseptr, *cbaseptr;
	        
	abaseptr = aml_scratch_baseptr(&sa);
	bbaseptr = aml_scratch_baseptr(&sb);
	ai = -1; bi = -1;
	
	ap = a;
	bp = b;
	cp = c;

	struct aml_scratch_request *ar, *br, *cr, *cr2;

	tilesPerCol = rowSizeInTiles / numthreads; //This should evaluate to an integer value
		
	//This will iterate through each column of tiles in A matrix
	//This loop is O(rowSizeInTiles) (O(n))
	for(i = 0; i < rowSizeInTiles; i++) {
		
		//Request next column of tiles from A into the scratchpad for A	
		
		oldbi = bi;
		oldai = ai;
		aml_scratch_async_pull(&sa, &ar, abaseptr, &ai, a, i + 1);
		aml_scratch_async_pull(&sb, &br, bbaseptr, &bi, b, i + 1);	
		//This will begin the dispersion of work accross threads, this loop is actually O(1) 
		#pragma omp parallel for
		for(k = 0; k < numthreads; k++)
		{
			int j, l, offset;
			double *apLoc, *bpLoc, *cpLoc;
			//This loop will cover if threads are handling multiple rows of tiles. This shouldn't be a necessarily large number
			//This loop is technically O(n) but in reality will usually be O(1) because tilesPerCol will be a small number relative to rowSizeInTiles
			for(j = 0; j < tilesPerCol; j++){
				offset = (k * tilesPerCol) + j;
				//This will give the beginning offset for where each thread should point to in the tilingB sized ap array
				apLoc = aml_tiling_tilestart(&tiling, ap, offset);
				offset = (k * tilesPerCol) + j;		
				//Now we will iterate through all the tiles in the row of B tiles and compute a partial matrix multiplication
				//This loop is O(n)
				for(l = 0; l < rowSizeInTiles; l++){
					bpLoc = aml_tiling_tilestart(&tiling, bp, l);
					//This will begin at the tile row that is at x cordinate equal to bp offset, and y cordinate equal to ap offset
					offset = (((int)rowSizeInTiles * ((k * tilesPerCol)+ j) ) + l);
					cp = aml_tiling_tilestart(&tiling, c, offset);

					cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, ldc, lda, ldb, 1.0, apLoc, lda, bpLoc, ldb, 1.0, cp, ldc);
				}			
			}
			
		}	
						
		aml_scratch_wait(&sa, ar);
		aml_scratch_wait(&sb, br);
		ap = aml_tiling_tilestart(&tilingB, abaseptr, ai);
		bp = aml_tiling_tilestart(&tilingB, bbaseptr, bi);
		aml_scratch_release(&sa, oldai);
		aml_scratch_release(&sb, oldbi);
	}
	
}

int argoMM(int argc, char* argv[]){
		AML_BINDING_SINGLE_DECL(binding);
		AML_ARENA_JEMALLOC_DECL(arena);
		AML_DMA_LINUX_SEQ_DECL(dma);
		unsigned long nodemask[AML_NODEMASK_SZ];
		aml_init(&argc, &argv);
		esize = (unsigned long)MEMSIZE/sizeof(double);
		numRows = (unsigned long)sqrt(esize);
	
		#pragma omp parallel
		{
			rowLengthInBytes = (unsigned long)sqrt(MEMSIZE/sizeof(double)) * sizeof(double);
			numthreads = omp_get_num_threads();
			tilesz = ((unsigned long) pow( ( ( sqrt(MEMSIZE / sizeof(double)) ) / (numthreads * 2) ), 2) ) * sizeof(double);
			double multiplier = 2;
			if(argc == 4){
				tilesz = sizeof(double)*(atol(argv[3]) * atol(argv[3]));
			}
			numTiles = MEMSIZE / tilesz; 
			CHUNKING = numTiles / numthreads;
			esz = tilesz/sizeof(double);
		}
	
			
		if(DEBUG || VERBOSE)printf("The total memory size is: %lu\nWe are dealing with a %lu x %lu matrix multiplication\nThe number of threads: %d\nThe chunking is: %lu\nThe tilesz is: %lu\nThat means there are %lu elements per tile\nThere are %lu tiles total\nThe length of a column in bytes is: %lu\n", MEMSIZE, (unsigned long)sqrt(MEMSIZE/sizeof(unsigned long)), (unsigned long)sqrt(MEMSIZE/sizeof(unsigned long)),numthreads, CHUNKING, tilesz, esz, numTiles, rowLengthInBytes);

		assert(!aml_binding_init(&binding, AML_BINDING_TYPE_SINGLE, 0));
		assert(!aml_tiling_init(&tiling, AML_TILING_TYPE_2D, (unsigned long)sqrt(tilesz/sizeof(double))*sizeof(double), (unsigned long)sqrt(tilesz/sizeof(double))*sizeof(double), tilesz, MEMSIZE));	
		rowSizeOfTile = aml_tiling_tilerowsize(&tiling, 0) / sizeof(double); 
		rowSizeInTiles = numRows / rowSizeOfTile;
		assert(!aml_tiling_init(&tilingB, AML_TILING_TYPE_2D, rowSizeOfTile * sizeof(double), rowSizeInTiles * rowSizeOfTile * sizeof(double), tilesz * rowSizeInTiles, MEMSIZE));
		AML_NODEMASK_ZERO(nodemask);
		AML_NODEMASK_SET(nodemask, 0);
		assert(!aml_arena_jemalloc_init(&arena, AML_ARENA_JEMALLOC_TYPE_REGULAR));
	
		assert(!aml_area_linux_init(&slow,
					    AML_AREA_LINUX_MANAGER_TYPE_SINGLE,
					    AML_AREA_LINUX_MBIND_TYPE_REGULAR,
					    AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS,
					    &arena, MPOL_BIND, nodemask));
		AML_NODEMASK_ZERO(nodemask);
		AML_NODEMASK_SET(nodemask, 1);
		assert(!aml_area_linux_init(&fast,
					    AML_AREA_LINUX_MANAGER_TYPE_SINGLE,
					    AML_AREA_LINUX_MBIND_TYPE_REGULAR,
					    AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS,
					    &arena, MPOL_BIND, nodemask));
		
		assert(!aml_dma_linux_seq_init(&dma, 3));
		assert(!aml_scratch_par_init(&sa, &fast, &slow, &dma, &tilingB, 2, 2));
		assert(!aml_scratch_par_init(&sb, &fast, &slow, &dma, &tilingB, 2, 2));
		
		/* allocation */
		a = aml_area_malloc(&slow, MEMSIZE);
		b = aml_area_malloc(&slow, MEMSIZE);
		c = aml_area_malloc(&fast, MEMSIZE);
		
		assert(a != NULL && b != NULL && c != NULL);
		esize = MEMSIZE/sizeof(double);
		numRows = (unsigned long)sqrt(esize);
		for(unsigned long i = 0; i < esize; i++) {
			a[i] = 1.0;//i % numRows;
			b[i] = 1.0;//numRows - (i % numRows);
			c[i] = 0.0;
		}	
		int newLines = 0;	
		//This will execute on core 0
		clock_gettime(CLOCK_REALTIME, &startClock);
		beginTime = rdtsc();
	
		do_work();
	
		//This will execute on core 0
		endTime = rdtsc();
		clock_gettime(CLOCK_REALTIME, &endClock);

		elapsedTime = BILLION * ( endClock.tv_sec - startClock.tv_sec ) + (( endClock.tv_nsec - startClock.tv_nsec ));

		//Prints RDTSC then CLOCK
		printf("%lu\t%lf\n", endTime - beginTime, elapsedTime);
	
		/* validate */
		unsigned long correct = 1;
		for(unsigned long i = 0; i < esize; i++){
			if(c[0] != c[i]){
				correct = 0;
			}
		}
	
		if(!correct){
			printf("The matrix multiplication failed. The last incorrect result is at location C(0,0) = %lf in the C matrix\n", c[0]);
		}	
	
		aml_scratch_par_destroy(&sa);
		aml_scratch_par_destroy(&sb);
		aml_dma_linux_seq_destroy(&dma);
		aml_area_free(&slow, a);
		aml_area_free(&slow, b);
		aml_area_free(&fast, c);
		aml_area_linux_destroy(&slow);
		aml_area_linux_destroy(&fast);
		aml_tiling_destroy(&tiling, AML_TILING_TYPE_1D);
		aml_tiling_destroy(&tilingB, AML_TILING_TYPE_2D);
		aml_binding_destroy(&binding, AML_BINDING_TYPE_SINGLE);
		aml_finalize();
}




//This matrix multiplication will implement matrix multiplication in the following way:
//	The A, B, and C matrices will be broken into tiles that edge as close to 1 MB as possible (size of L2 cache) while also allowing all threads to work on data.
//	The algorithm will chunk up the work dependent on number of tiles. The multiplication will go as follows:
//		1) The algorithm will take a column of the tiles of A. (Prefetch next column of A tiles to fast memory).
//		2) The algorithm will take a column of B tiles (prefetch next column into fast memory).
//		3) Perform partial matrix multiplications using A tile and B Tile (using dgemm). 
//		4) Repeat partial matrix multiplications until A and B columns are exhausted.
//		5) DONE
//Another potential solution could be to tile the B matrix as well. This will require Atomic Additions though.  
int main(int argc, char *argv[])
{
	if(argc == 1){
		if(VERBOSE) printf("No arguments provided, setting numThreads = %d and Memsize = %lu\n", NUMBER_OF_THREADS, MEMSIZE);
		omp_set_num_threads(NUMBER_OF_THREADS);
		MEMSIZE = MEMSIZES;
	}
	else if(argc == 2){	
		if(VERBOSE) printf("Setting number of threads\n");
		omp_set_num_threads(NUMBER_OF_THREADS);
	 	if(VERBOSE) printf("Setting MEMSIZE\n");	
		MEMSIZE = sizeof(double)*(atol(argv[1]) * atol(argv[1]));
		if(VERBOSE) printf("1 argument provided, setting numThreads = %d and Memsize = %lu\n", NUMBER_OF_THREADS, MEMSIZE);
	}
	else if(argc >= 3){
		omp_set_num_threads(atoi(argv[2]));
		MEMSIZE = sizeof(double)*(atol(argv[1]) * atol(argv[1]));
		if(VERBOSE) printf("Two arguments provided, setting numThreads = %d and Memsize = %lu\n", atoi(argv[2]), atol(argv[1]));
	}

	argoMM(argc, argv);

	return 0;
}