首页 > 其他分享 >code

code

时间:2022-11-07 17:03:09浏览次数:36  
标签:function code int ll transpose && include

点击查看代码
//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;
}

标签:function,code,int,ll,transpose,&&,include
From: https://www.cnblogs.com/loadingkkk/p/16866542.html

相关文章

  • sql中decode函数用法
    decode(字段或字段的运算,值1,值2,值3)      这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回值3 当然值1,值2,值3也可以是表达式,这个函数......
  • CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)
    我要死给普,给我死给普A记得好像开场不会来着。就只要判断\(a_1\)是否为\(1\)就行了。正确性显然。B分讨两种情况,一种是所有的01都算上,另一种是取全0或全1段,......
  • Codeforces Subsequences
    题目:KarllikesCodeforcesandsubsequences.HewantstofindastringoflowercaseEnglishlettersthatcontainsatleastksubsequencescodeforces.Outofall......
  • encodeuricomponent有什么用?
    encodeuricomponent有什么用? 1、encodeuricomponent可把字符串作为URI组件进行编码。该方法不会对ASCII字母和数字进行编码,也不会对这些ASCII标点符号进行编码......
  • CodeTON Round 3 (C.差分维护,D.容斥原理)
    C.ComplementaryXOR题目大意:给你两个01串ab,问你是否可以通过一下两种操作在不超过n+5次的前提下将两个串都变为0,同时需要输出可以的操作方案选择一个区间[l,r]将......
  • VSCode使用笔记
    官网下载下载慢解决方式点击下载获取下载地址这个时候直接复制vscode.cdn.azure.cn替换地址上面的下载......
  • VS Code快捷键及前端常用插件
    VSCode快捷键放大缩小视图:ctrl++和ctrl+-向上复制一行:alt+shift+↑向下复制一行:alt+shift+↓当光标点击到某一行是,默认选中全行,可以直接复制剪切VSCo......
  • VsCode预定义全局变量使用
    VsCode预定义全局变量使用在VsCode的launch.json和tasks.json中我们常用到一些全局变量,同时为了修改配置文件方便,还想自定义一些全局变量,这里做一下介绍。预定义全局变量......
  • Codeforces Round #721 (Div. 2) B2. Palindrome Game (hard version)
    ​​传送门​​Theonlydifferencebetweentheeasyandhardversionsisthatthegivenstringsintheeasyversionisinitiallyapalindrome,thisconditioni......
  • Codeforces Round #713 (Div. 3) F
    F.Education考虑贪心显然我们每次只有这样一种情况就是钱够了就升级然后到一个位置就一直不动了不可能我们先在一个位置钱赚够了再赚几轮再去下一级那么证明我们......