点击查看代码
//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;
}
点击查看代码
#include <stdio.h>
#include "cachelab.h"
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <limits.h>
long s,S,E,b,B,C,t;
int hit_count=0, miss_count=0, eviction_count=0;
int atime=0;
char pth[100];// 存 getopt 中选项内容,表示的是验证中需使用的trace文件名
typedef struct{
int valid;
long tag;
int stamp;
}line, *set, **cache; // cache 模拟器的结构。由合法位、标记位和时间戳组成
cache cash = NULL; // 声明一个空的结构体类型二维数组
void init_cache()
{
//多维数组的开辟要一行行malloc
cash = (cache)malloc(sizeof(set) * S);
for(int i = 0; i < S; ++i)
{
cash[i] = (set)malloc(sizeof(line) * E);
for(int j = 0; j < E; ++j)
{
cash[i][j].valid = 0;
cash[i][j].tag = -1;
cash[i][j].stamp = -1;
}
}
}
void update(unsigned long address)
{
// 索引地址位可以用位运算,-1U是最大整数,64是因为我电脑是64位
int set1 = (address >> b) & ((-1U) >> (64 - s));
long tag1 = address >> (b + s);
int min_stamp = atime+1;
int min_stamp_index = -1;
for(int i = 0; i < E; ++i) //如果tag相同,就hit,重置时间戳
{
if(cash[set1][i].tag == tag1)
{
cash[set1][i].stamp = ++atime;
++hit_count;
return ;
}
}
for(int i = 0; i < E; ++i) // 查看有没有空行
{
if(cash[set1][i].valid == 0)
{
cash[set1][i].valid = 1;
cash[set1][i].tag = tag1;
cash[set1][i].stamp = ++atime;
++miss_count;
return ;
}
}
// 没有空行又没有hit就是要替换了
++eviction_count;
++miss_count;
for(int i = 0; i < E; ++i)
{
if(cash[set1][i].stamp < min_stamp)
{
min_stamp = cash[set1][i].stamp;
min_stamp_index = i;
}
}
cash[set1][min_stamp_index].tag = tag1;
cash[set1][min_stamp_index].stamp = ++atime;
return ;
}
void end_cash(){
for(int i=1;i<=S;++i) free(cash[i]);
free(cash);
}
int main(int argc,char *argv[]){
char ch;
while((ch=getopt(argc,argv,"s:E:b:t:"))!=-1){
switch(ch){
case 's':{
s=atoi(optarg);
break;
}
case 'E':{
E=atoi(optarg);
break;
}
case 'b':{
b=atoi(optarg);
break;
}
case 't':{
int len=strlen(optarg);
for(int i=0;i<len;++i)
pth[i]=optarg[i];
}
default:
break;
}
}
t=64-s-b;
S=1<<s;
init_cache();
FILE* fp = fopen(pth, "r");
char operation;
unsigned long address;
int size;
while(fscanf(fp, " %c %lx,%d\n", &operation, &address, &size) > 0)
{
switch(operation)
{
case 'I': continue;
case 'L':{
update(address);
break;}
case 'M':
update(address); // miss的话还要进行一次storage
case 'S':
update(address);
}
}
end_cash();
printSummary(hit_count, miss_count, eviction_count);
return 0;
}
点击查看代码
#include<bits/stdc++.h>
#define N 555555
using namespace std;
char s[N];
int n,m;
char c[N];
int dep[N];
int ch[N][2],tag[N];
inline void init(int t){
ch[t][0]=ch[t][1]=0;
c[t]=0;dep[t]=0;
tag[t]=0;return;
}
inline void qian(int g){
if(!g)return;
putchar(c[g]);
if(ch[g][0])qian(ch[g][0]);
if(ch[g][1])qian(ch[g][1]);
}
inline void zhong(int g){
if(!g)return;
if(ch[g][0])zhong(ch[g][0]);
putchar(c[g]);
if(ch[g][1])zhong(ch[g][1]);
}
inline void hou(int g){
if(!g)return;
if(ch[g][0])hou(ch[g][0]);
if(ch[g][1])hou(ch[g][1]);
putchar(c[g]);
}
inline void work(){
n=0;
while(1){
scanf("%s",s+1);
if(s[1]=='0')break;
++n;
init(n);
int len=strlen(s+1);
if(n==1){
c[1]=s[1];
dep[1]=1;
}
else if(s[len]=='*'){
tag[n-1]=1;
}
else{
c[n]=s[len];
dep[n]=len;
int t=n-1;
while(dep[t]!=dep[n]-1)--t;
// printf("link:%d %d\n",t,n);
ch[t][tag[t]]=n;++tag[t];
}
}
qian(1);puts("");
hou(1);puts("");
zhong(1);puts("");
puts("");
}
int main(){
int T;cin>>T;
while(T--)work();
}
点击查看代码
#include<bits/stdc++.h>
#define N 555555
using namespace std;
int a[N],n;
inline void work(int zl,int zr,int hl,int hr){
if(zl>zr)return;
if(zl==zr){printf("%d ",a[zl]);return;}
int t=zl;
for(int i=zl;i<=zr;++i){
if(a[i]==a[hr]){
t=i;break;
}
}
printf("%d ",a[t]);
work(zl,t-1,hl,hl+(t-zl)-1);
work(t+1,zr,hl+(t-zl),hr-1);
return;
}
int main(){
n=1;
while(scanf("%d",&a[n])!=EOF){
++n;
//if(n==9)break;
}
n/=2;
work(1,n,n+1,n+n);
}
点击查看代码
#include<bits/stdc++.h>
#define N 555555
using namespace std;
priority_queue<int>q;
inline void work(){
int n;cin>>n;
while(!q.empty())q.pop();
for(int i=1,op,x;i<=n;++i){
scanf("%d",&op);
if(op==1){
scanf("%d",&x);
q.push(-x);
}
else{
printf("%d\n",-q.top());
q.pop();
}
}
}
int main(){
int T;cin>>T;
while(T--)work();
}