点击查看代码
//Zhang Kaitai 2100012922
#include <stdio.h>
#include "cachelab.h"
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <limits.h>
#define ll long
char s[3],s2[100],s3,c;
ll f[555];
ll ls,S,E,lb,B,C,t;
char pth[100];
int hit_count, miss_count, eviction_count;
struct cache{
ll t,st;
}**cash;
ll altime;
//cache cash[32][5][32];
void init_cash(){
cash=(struct cache**) malloc(sizeof(struct cache*)*S);
for(ll i=0;i<S;++i)
cash[i]=(struct cache*)malloc(sizeof(struct cache)*E);
}
void end_cash(){
for(int i=1;i<=S;++i)free(cash[i]);
free(cash);
}
ll getv(char *s){
ll len=strlen(s),a=0;
for(ll i=0;i<len;++i)
a=a*10+s[i]-'0';
return a;
}
void getval2(char *a,char *s){
int len=strlen(s);
for(int i=0;i<len;++i)a[i]=s[i];
return;
}
void init(){
for(int i='0';i<='9';++i)f[i]=i-'0';
for(int i='a';i<='f';++i)f[i]=i-'a'+10;
}
ll pos;int sz;
void check(ll pos){
ll k=((1ll<<ls)-1);k=k&(pos>>lb);
ll tag=((1ll<<t)-1);tag=tag&(pos>>(ls+lb));
ll bs=(1ll<<lb)-1;bs=bs&pos;
int fr=0;
for(int i=0;i<E;++i){
if(cash[k][i].t&&cash[k][i].st==tag){
++hit_count;cash[k][i].t=++altime;
return;
}
if(cash[k][i].t<cash[k][fr].t)fr=i;
}
++miss_count;
if(cash[k][fr].t)++eviction_count;
cash[k][fr].t=++altime;
cash[k][fr].st=tag;
}
void work(){
if(s[0]=='I')return;
check(pos);
if(s[0]=='M')check(pos);
}
int main(int argc,char *argv[]){
init();
char ch;
while((ch=getopt(argc,argv,"s:E:b:t:"))!=-1){
switch(ch){
case 's':{
ls=getv(optarg);
break;
}
case 'E':{
E=getv(optarg);
break;
}
case 'b':{
lb=getv(optarg);
break;
}
case 't':{
getval2(pth,optarg);
}
default: break;
}
}
t=64-ls-lb;
S=1<<ls;B=1<<lb;C=S*E*B;
init_cash();
freopen(pth,"r",stdin);
while(scanf("%s",s)!=EOF){
pos=sz=0;
c=getchar();
while((c<'a'||c>'f')&&(c>'9'||c<'0'))c=getchar();
while((c>='a'&&c<='f')||(c>='0'&&c<='9')){
pos=(pos<<4)+f[(int)c];
c=getchar();
}
while((c<'a'||c>'f')&&(c>'9'||c<'0'))c=getchar();
while((c>='a'&&c<='f')||(c>='0'&&c<='9')){
sz=sz*10+c-'0';
c=getchar();
}
work();
}
end_cash();
printSummary(hit_count, miss_count, eviction_count);
return 0;
}
点击查看代码
/*
* Zhang Kaitai
* 2100012922
* 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"
#include "contracts.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. The REQUIRES and ENSURES from 15-122 are included
* for your convenience. They can be removed if you like.
*/
char transpose_submit_desc[] = "Transpose submission";
void transpose_submit(int M, int N, int A[N][M], int B[M][N])
{
REQUIRES(M > 0);
REQUIRES(N > 0);
int i,j,k,o,tmp;//t1,t2;
#define pad 2
if(N==M){
// for(i=0;i<N;++i)for(j=0;j<M;++j)B[i][j]=A[i][j];
for(o=0;o<N;o+=8){
for(k=0;k<N;k+=pad){
for(i=o;i<o+8;++i){
for(j=k;j<k+pad;++j){
B[j][i]=A[i][j];
// B[j+1][i]=A[i][j+1];
// B[j+2][i]=A[i][j+2];
// B[j+3][i]=A[i][j+3];
}
}
}
}
}
else{
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
tmp = A[i][j];
B[j][i] = tmp;
}
}
}
ENSURES(is_transpose(M, N, A, B));
}
/*
* 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;
REQUIRES(M > 0);
REQUIRES(N > 0);
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
tmp = A[i][j];
B[j][i] = tmp;
}
}
ENSURES(is_transpose(M, N, A, B));
}
/*
* 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;
}