Browse Source

Part B

homework5
邓博昊 6 months ago
parent
commit
196af01046
10 changed files with 567631 additions and 1 deletions
  1. +15
    -1
      Makefile
  2. BIN
      aqua-handin.tar
  3. BIN
      csim
  4. BIN
      test-trans
  5. +261
    -0
      test-trans.c
  6. +567064
    -0
      trace.tmp
  7. BIN
      tracegen
  8. +108
    -0
      tracegen.c
  9. +183
    -0
      trans.c
  10. BIN
      trans.o

+ 15
- 1
Makefile View File

@ -5,11 +5,22 @@
CC = gcc
CFLAGS = -g -Wall -Werror -std=c99 -m64
all: csim
all: csim test-trans tracegen
# Generate a handin tar file each time you compile
-tar -cvf ${USER}-handin.tar csim.c trans.c
csim: csim.c cachelab.c cachelab.h
$(CC) $(CFLAGS) -o csim csim.c cachelab.c -lm
test-trans: test-trans.c trans.o cachelab.c cachelab.h
$(CC) $(CFLAGS) -o test-trans test-trans.c cachelab.c trans.o
tracegen: tracegen.c trans.o cachelab.c
$(CC) $(CFLAGS) -O0 -o tracegen tracegen.c trans.o cachelab.c
trans.o: trans.c
$(CC) $(CFLAGS) -O0 -c trans.c
#
# Clean the src dirctory
#
@ -17,3 +28,6 @@ clean:
rm -rf *.o
rm -f *.tar
rm -f csim
rm -f test-trans tracegen
rm -f trace.all trace.f*
rm -f .csim_results .marker

BIN
aqua-handin.tar View File


BIN
csim View File


BIN
test-trans View File


+ 261
- 0
test-trans.c View File

@ -0,0 +1,261 @@
/*
* test-trans.c - Checks the correctness and performance of all of the
* student's transpose functions and records the results for their
* official submitted version as well.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <sys/types.h>
#include "cachelab.h"
#include <sys/wait.h> // fir WEXITSTATUS
#include <limits.h> // for INT_MAX
/* Maximum array dimension */
#define MAXN 256
/* The description string for the transpose_submit() function that the
student submits for credit */
#define SUBMIT_DESCRIPTION "Transpose submission"
/* External function defined in trans.c */
extern void registerFunctions();
/* External variables defined in cachelab-tools.c */
extern trans_func_t func_list[MAX_TRANS_FUNCS];
extern int func_counter;
/* Globals set on the command line */
static int M = 0;
static int N = 0;
/* The correctness and performance for the submitted transpose function */
struct results {
int funcid;
int correct;
int misses;
};
static struct results results = {-1, 0, INT_MAX};
/*
* eval_perf - Evaluate the performance of the registered transpose functions
*/
void eval_perf(unsigned int s, unsigned int E, unsigned int b)
{
int i,flag;
unsigned int len, hits, misses, evictions;
unsigned long long int marker_start, marker_end, addr;
char buf[1000], cmd[255];
char filename[128];
registerFunctions();
/* Open the complete trace file */
FILE* full_trace_fp;
FILE* part_trace_fp;
/* Evaluate the performance of each registered transpose function */
for (i=0; i<func_counter; i++) {
if (strcmp(func_list[i].description, SUBMIT_DESCRIPTION) == 0 )
results.funcid = i; /* remember which function is the submission */
printf("\nFunction %d (%d total)\nStep 1: Validating and generating memory traces\n",i,func_counter);
/* Use valgrind to generate the trace */
sprintf(cmd, "valgrind --tool=lackey --trace-mem=yes --log-fd=1 -v ./tracegen -M %d -N %d -F %d > trace.tmp", M, N,i);
flag=WEXITSTATUS(system(cmd));
if (0!=flag) {
printf("Validation error at function %d! Run ./tracegen -M %d -N %d -F %d for details.\nSkipping performance evaluation for this function.\n",flag-1,M,N,i);
continue;
}
/* Get the start and end marker addresses */
FILE* marker_fp = fopen(".marker", "r");
assert(marker_fp);
fscanf(marker_fp, "%llx %llx", &marker_start, &marker_end);
fclose(marker_fp);
func_list[i].correct=1;
/* Save the correctness of the transpose submission */
if (results.funcid == i ) {
results.correct = 1;
}
full_trace_fp = fopen("trace.tmp", "r");
assert(full_trace_fp);
/* Filtered trace for each transpose function goes in a separate file */
sprintf(filename, "trace.f%d", i);
part_trace_fp = fopen(filename, "w");
assert(part_trace_fp);
/* Locate trace corresponding to the trans function */
flag = 0;
while (fgets(buf, 1000, full_trace_fp) != NULL) {
/* We are only interested in memory access instructions */
if (buf[0]==' ' && buf[2]==' ' &&
(buf[1]=='S' || buf[1]=='M' || buf[1]=='L' )) {
sscanf(buf+3, "%llx,%u", &addr, &len);
/* If start marker found, set flag */
if (addr == marker_start)
flag = 1;
/* Valgrind creates many spurious accesses to the
stack that have nothing to do with the students
code. At the moment, we are ignoring all stack
accesses by using the simple filter of recording
accesses to only the low 32-bit portion of the
address space. At some point it would be nice to
try to do more informed filtering so that would
eliminate the valgrind stack references while
include the student stack references. */
if (flag && addr < 0xffffffff) {
fputs(buf, part_trace_fp);
}
/* if end marker found, close trace file */
if (addr == marker_end) {
flag = 0;
fclose(part_trace_fp);
break;
}
}
}
fclose(full_trace_fp);
/* Run the reference simulator */
printf("Step 2: Evaluating performance (s=%d, E=%d, b=%d)\n", s, E, b);
char cmd[255];
sprintf(cmd, "./csim-ref -s %u -E %u -b %u -t trace.f%d > /dev/null",
s, E, b, i);
system(cmd);
/* Collect results from the reference simulator */
FILE* in_fp = fopen(".csim_results","r");
assert(in_fp);
fscanf(in_fp, "%u %u %u", &hits, &misses, &evictions);
fclose(in_fp);
func_list[i].num_hits = hits;
func_list[i].num_misses = misses;
func_list[i].num_evictions = evictions;
printf("func %u (%s): hits:%u, misses:%u, evictions:%u\n",
i, func_list[i].description, hits, misses, evictions);
/* If it is transpose_submit(), record number of misses */
if (results.funcid == i) {
results.misses = misses;
}
}
}
/*
* usage - Print usage info
*/
void usage(char *argv[]){
printf("Usage: %s [-h] -M <rows> -N <cols>\n", argv[0]);
printf("Options:\n");
printf(" -h Print this help message.\n");
printf(" -M <rows> Number of matrix rows (max %d)\n", MAXN);
printf(" -N <cols> Number of matrix columns (max %d)\n", MAXN);
printf("Example: %s -M 8 -N 8\n", argv[0]);
}
/*
* sigsegv_handler - SIGSEGV handler
*/
void sigsegv_handler(int signum){
printf("Error: Segmentation Fault.\n");
printf("TEST_TRANS_RESULTS=0:0\n");
fflush(stdout);
exit(1);
}
/*
* sigalrm_handler - SIGALRM handler
*/
void sigalrm_handler(int signum){
printf("Error: Program timed out.\n");
printf("TEST_TRANS_RESULTS=0:0\n");
fflush(stdout);
exit(1);
}
/*
* main - Main routine
*/
int main(int argc, char* argv[])
{
char c;
while ((c = getopt(argc,argv,"M:N:h")) != -1) {
switch(c) {
case 'M':
M = atoi(optarg);
break;
case 'N':
N = atoi(optarg);
break;
case 'h':
usage(argv);
exit(0);
default:
usage(argv);
exit(1);
}
}
if (M == 0 || N == 0) {
printf("Error: Missing required argument\n");
usage(argv);
exit(1);
}
if (M > MAXN || N > MAXN) {
printf("Error: M or N exceeds %d\n", MAXN);
usage(argv);
exit(1);
}
/* Install SIGSEGV and SIGALRM handlers */
if (signal(SIGSEGV, sigsegv_handler) == SIG_ERR) {
fprintf(stderr, "Unable to install SIGALRM handler\n");
exit(1);
}
if (signal(SIGALRM, sigalrm_handler) == SIG_ERR) {
fprintf(stderr, "Unable to install SIGALRM handler\n");
exit(1);
}
/* Time out and give up after a while */
alarm(120);
/* Check the performance of the student's transpose function */
eval_perf(5, 1, 5);
/* Emit the results for this particular test */
if (results.funcid == -1) {
printf("\nError: We could not find your transpose_submit() function\n");
printf("Error: Please ensure that description field is exactly \"%s\"\n",
SUBMIT_DESCRIPTION);
printf("\nTEST_TRANS_RESULTS=0:0\n");
}
else {
printf("\nSummary for official submission (func %d): correctness=%d misses=%d\n",
results.funcid, results.correct, results.misses);
printf("\nTEST_TRANS_RESULTS=%d:%d\n", results.correct, results.misses);
}
return 0;
}

+ 567064
- 0
trace.tmp
File diff suppressed because it is too large
View File


BIN
tracegen View File


+ 108
- 0
tracegen.c View File

@ -0,0 +1,108 @@
/*
* tracegen.c - Running the binary tracegen with valgrind produces
* a memory trace of all of the registered transpose functions.
*
* The beginning and end of each registered transpose function's trace
* is indicated by reading from "marker" addresses. These two marker
* addresses are recorded in file for later use.
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <getopt.h>
#include "cachelab.h"
#include <string.h>
/* External variables declared in cachelab.c */
extern trans_func_t func_list[MAX_TRANS_FUNCS];
extern int func_counter;
/* External function from trans.c */
extern void registerFunctions();
/* Markers used to bound trace regions of interest */
volatile char MARKER_START, MARKER_END;
static int A[256][256];
static int B[256][256];
static int M;
static int N;
int validate(int fn,int M, int N, int A[N][M], int B[M][N]) {
int C[M][N];
memset(C,0,sizeof(C));
correctTrans(M,N,A,C);
for(int i=0;i<M;i++) {
for(int j=0;j<N;j++) {
if(B[i][j]!=C[i][j]) {
printf("Validation failed on function %d! Expected %d but got %d at B[%d][%d]\n",fn,C[i][j],B[i][j],i,j);
return 0;
}
}
}
return 1;
}
int main(int argc, char* argv[]){
int i;
char c;
int selectedFunc=-1;
while( (c=getopt(argc,argv,"M:N:F:")) != -1){
switch(c){
case 'M':
M = atoi(optarg);
break;
case 'N':
N = atoi(optarg);
break;
case 'F':
selectedFunc = atoi(optarg);
break;
case '?':
default:
printf("./tracegen failed to parse its options.\n");
exit(1);
}
}
/* Register transpose functions */
registerFunctions();
/* Fill A with data */
initMatrix(M,N, A, B);
/* Record marker addresses */
FILE* marker_fp = fopen(".marker","w");
assert(marker_fp);
fprintf(marker_fp, "%llx %llx",
(unsigned long long int) &MARKER_START,
(unsigned long long int) &MARKER_END );
fclose(marker_fp);
if (-1==selectedFunc) {
/* Invoke registered transpose functions */
for (i=0; i < func_counter; i++) {
MARKER_START = 33;
(*func_list[i].func_ptr)(M, N, A, B);
MARKER_END = 34;
if (!validate(i,M,N,A,B))
return i+1;
}
} else {
MARKER_START = 33;
(*func_list[selectedFunc].func_ptr)(M, N, A, B);
MARKER_END = 34;
if (!validate(selectedFunc,M,N,A,B))
return selectedFunc+1;
}
return 0;
}

+ 183
- 0
trans.c View File

@ -0,0 +1,183 @@
/*
* trans.c - Matrix transpose B = A^T
*
* Each transpose function must have a prototype of the form:
* void trans(int M, int N, int A[N][M], int B[M][N]);
*
* A transpose function is evaluated by counting the number of misses
* on a 1KB direct mapped cache with a block size of 32 bytes.
*/
#include <stdio.h>
#include "cachelab.h"
int is_transpose(int M, int N, int A[N][M], int B[M][N]);
/*
* transpose_submit - This is the solution transpose function that you
* will be graded on for Part B of the assignment. Do not change
* the description string "Transpose submission", as the driver
* searches for that string to identify the transpose function to
* be graded.
*/
char transpose_submit_desc[] = "Transpose submission";
void transpose_submit(int M, int N, int A[N][M], int B[M][N])
{
int i, j, k, l, a0, a1, a2, a3, a4, a5, a6, a7;
if(M == 32){
for (i = 0; i < N; i+=8) {
for (j = 0; j < M; j+=8) {
if(i == j){
for(k = i ;k < i + 8 && k<N;k++){
a0 = A[k][j];
a1 = A[k][j+1];
a2 = A[k][j+2];
a3 = A[k][j+3];
a4 = A[k][j+4];
a5 = A[k][j+5];
a6 = A[k][j+6];
a7 = A[k][j+7];
B[j][k] = a0;
B[j+1][k] = a1;
B[j+2][k] = a2;
B[j+3][k] = a3;
B[j+4][k] = a4;
B[j+5][k] = a5;
B[j+6][k] = a6;
B[j+7][k] = a7;
}
}
else{
for(k = i ;k < i + 8 && k<N;k++){
for(l = j ; l < j + 8 && l < M;l++)
B[l][k] = A[k][l];
}
}
}
}
}
else if(M == 64){
for (i = 0; i < N; i += 8) {
for (j = 0; j < M; j += 8) {
for (k = i; k < i + 4; k++) {
a0 = A[k][j];
a1 = A[k][j + 1];
a2 = A[k][j + 2];
a3 = A[k][j + 3];
a4 = A[k][j + 4];
a5 = A[k][j + 5];
a6 = A[k][j + 6];
a7 = A[k][j + 7];
B[j][k] = a0;
B[j + 1][k] = a1;
B[j + 2][k] = a2;
B[j + 3][k] = a3;
B[j][k + 4] = a4;
B[j + 1][k + 4] = a5;
B[j + 2][k + 4] = a6;
B[j + 3][k + 4] = a7;
}
for (l = j + 4; l < j + 8; l++) {
a4 = A[i + 4][l - 4]; // A left-down col
a5 = A[i + 5][l - 4];
a6 = A[i + 6][l - 4];
a7 = A[i + 7][l - 4];
a0 = B[l - 4][i + 4]; // B right-above line
a1 = B[l - 4][i + 5];
a2 = B[l - 4][i + 6];
a3 = B[l - 4][i + 7];
B[l - 4][i + 4] = a4; // set B right-above line
B[l - 4][i + 5] = a5;
B[l - 4][i + 6] = a6;
B[l - 4][i + 7] = a7;
B[l][i] = a0; // set B left-down line
B[l][i + 1] = a1;
B[l][i + 2] = a2;
B[l][i + 3] = a3;
B[l][i + 4] = A[i + 4][l];
B[l][i + 5] = A[i + 5][l];
B[l][i + 6] = A[i + 6][l];
B[l][i + 7] = A[i + 7][l];
}
}
}
}
else if (M == 61){
for (i = 0; i < N; i += 16) {
for (j = 0; j < M; j += 16) {
for (k = i; k < i + 16&& k<N; k++) {
for(l =j ;l<j+16&&l<M;l++)
B[l][k] = A[k][l];
}
}
}
}
}
/*
* You can define additional transpose functions below. We've defined
* a simple one below to help you get started.
*/
/*
* trans - A simple baseline transpose function, not optimized for the cache.
*/
char trans_desc[] = "Simple row-wise scan transpose";
void trans(int M, int N, int A[N][M], int B[M][N])
{
int i, j, tmp;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
tmp = A[i][j];
B[j][i] = tmp;
}
}
}
/*
* registerFunctions - This function registers your transpose
* functions with the driver. At runtime, the driver will
* evaluate each of the registered functions and summarize their
* performance. This is a handy way to experiment with different
* transpose strategies.
*/
void registerFunctions()
{
/* Register your solution function */
registerTransFunction(transpose_submit, transpose_submit_desc);
/* Register any additional transpose functions */
registerTransFunction(trans, trans_desc);
}
/*
* is_transpose - This helper function checks if B is the transpose of
* A. You can check the correctness of your transpose by calling
* it before returning from the transpose function.
*/
int is_transpose(int M, int N, int A[N][M], int B[M][N])
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < M; ++j) {
if (A[i][j] != B[j][i]) {
return 0;
}
}
}
return 1;
}

BIN
trans.o View File


Loading…
Cancel
Save