MỤC LỤC
MỤC LỤC 1
Chương 1 4
GIỚI THIỆU SƠ NÉT VỀ ĐỀ TÀI 4
I. TỔNG QUAN 4
II. NHIỆM VỤ VÀ MỤC ĐÍCH CỦA ĐỀ TÀI 6
Chương 2 7
NGÔN NGỮ OCCAM CHO HỆ THỐNG TRANSPUTER 7
A. GIỚI THIỆU NGÔN NGỮ OCCAM 7
I. TỔNG QUAN 7
II. CÁC QUA TRÌNH ĐƠN GIẢN 8
III. CÁC CẤU TRÚC ĐIỀU KHIỂN 9
IV. CÁC KIỂU DỮ LIỆU VÀ KHAI BÁO BIẾN 16
V. KHAI BÁO KÊNH TRUYỀN ĐẶC TẢ CÁC NGHI THỨC 22
VI. CHƯƠNG TRÌNH CON 24
VII. CÁC KHAI BÁO TÓM TẮT 26
VIII. VẤN ĐỀ TẦM VỰC VÀ CÁC HÀM CÓ SẲN 26
B. GIỚI THIỆU HỆ THỐNG TRANSPUTER 28
I CẤU TRÚC TRANSPUTER 28
II QUI TRÌNH HOẠT ĐỘNG 29
III CẤU HÌNH LIÊN KẾT CỦA MỘT MẠNG TRANSPUTER 29
IV CHƯƠNG TRÌNH OCCAM HOẠT ĐỘNG TRÊN NHIỀU BỘ XỬ LÝ 30
Chương 3 31
MÔI TRƯỜNG MPI VÀ CƠ CHẾ MESSAGE PASSING 31
A. TỔNG QUAN VỀ MPI 31
B. NHỮNG ĐIỂM CƠ BẢN CỦA MPI 31
C. NHỮNG ĐIỂM CHÍNH CẦN QUAN TÂM KHI LÀM VIỆC VỚI MPI 31
I. GIAO TIẾP ĐIỂM - ĐIỂM ( POINT TO POINT) 33
II. COLLECTIVE COMMUNICATION 34
III. NHÓM CÁC PROCESS 34
IV. XÂY DỰNG GROUP 35
V. XÂY DỰNG CÁC COMMUNICATOR 35
VI. GIAO TIẾP GIỮA CÁC GROUP 36
VII. TRUY CẬP INTERCOMMUNICATORS 37
VIII. THAO TÁC INTERCOMMUNICATOR 37
IX. SỰ KẾT THÚC GIAO TIẾP 37
X. CÁC KIỂU GIAO TIẾP 37
XI. HÀNH VI BLOCKING VÀ NON-BLOCKING 39
XII. HÀM COMPLETION 39
XIII. MULTIPLE COMPLETION 39
XIV. GIAO TIẾP DỮ LIỆU KHÔNG CƠ BẢN ( NON-PRIMITIVE DATATYPES ) 40
XV. XÂY DỰNG KIỂU DỮ LIỆU 40
XVI. THAO TÁC SEND/RECEIVE VÀ GIAO TIẾP COLECTIVE 41
XVII. ỨNG DỤNG VÀ CÁC HÀM HỖ TRỢ TOPOLOGY 42
XVIII. SỬ DỤNG TOPOLOGY. 42
XIX. PACK VÀ UNPACK DỮ LIỆU 43
Chương 4 44
XÂY DỰNG TRÌNH BIÊN DỊCH 44
A. PHÂN TÍCH 44
I. PHƯƠNG HƯỚNG THỰC HIỆN 44
II. BỘ TIỀN XỬ LÝ 46
III. BỘ PHÂN TÍCH TỪ VỰNG 46
IV. BỘ PHÂN TÍCH CÚ PHÁP 47
V. VẤN ĐỀ SINH MÃ ĐỐI TƯỢNG 48
B. THIẾT KẾ 49
I. THIẾT KẾ BỘ TIỀN XỬ LÝ 49
II. THIẾT KẾ BỘ PHÂN TÍCH TỪ VỰNG 51
III. THIẾT KẾ BỘ PHÂN TÍCH CÚ PHÁP VÀ SINH MÃ 52
C. HIỆN THỰC CHƯƠNG TRÌNH DỊCH 55
I. TIỀN XỬ LÝ 55
II. PHÂN TÍCH TỪ VỰNG 56
III. PHÂN TÍCH CÚ PHÁP 56
IV. CHUYỂN ĐỔI TƯƠNG ĐƯƠNG OCCAM VÀ MPI TRONG QUÁ TRÌNH SINH MÃ 57
Chương 5 72
MÔ PHỎNG HỆ THỐNG TRANSPUTER TRÊN MÔI TRƯỜNG MPI. 72
I. SỰ CẦN THIẾT CỦA MÔ PHỎNG HỆ THỐNG 72
II. HƯỚNG MÔ PHỎNG 72
III. KHẢ NĂNG CỦA HỆ THỐNG THỰC 72
Chương 6 74
NHẬN XÉT ĐÁNH GIÁ : 74
1. MỘT SỐ KHẢ NĂNG CỦA CHƯƠNG TRÌNH 74
2. NHỮNG VẤN ĐỀ CHƯA LÀM ĐƯỢC 74
3. HƯỚNG PHÁT TRIỂN 74
Source code 76
File :List.H 181
File:Str.H 193
PHỤ LỤC A 198
Phụ lục B 198
Các ví dụ minh họa: 204
Tài liệu tham khảo 206
208 trang |
Chia sẻ: lvcdongnoi | Lượt xem: 2437 | Lượt tải: 1
Bạn đang xem trước 20 trang tài liệu Luận văn Mô phỏng hệ thống Transputer trên môi trường MPI, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
return INT64;
if (strstr(type,"float")) return REAL32;
if (strstr(type,"double")) return REAL64;
if (strstr(type,"Boolean")) return BOOL;
if (strstr(type,"unsigned char")) return BYTE;
if (strstr(type,"TIMER")) return TIMER;
return -1;
}
char *formattype(char *type)
{
if (strstr(type,"short")) {sprintf(type,"%s","%d\0");return type;}
if (strstr(type,"int")) {sprintf(type,"%s","%d\0");return type;}
if (strstr(type,"long")) {sprintf(type,"%s","%d\0");return type;}
if (strstr(type,"long long")) {sprintf(type,"%s","%d\0");return type;}
if (strstr(type,"float")) {sprintf(type,"%s","%f\0");return type;}
if (strstr(type,"double")) {sprintf(type,"%s","%f\0");return type;}
if (strstr(type,"Boolean")) {sprintf(type,"%s","%d\0");return type;}
if (strstr(type,"unsigned char")){sprintf(type,"%s","%d\0");return type;}
if (strstr(type,"TIMER")) {sprintf(type,"%s","%d\0");return type;}
}
int Checkarr(char *st1, char *st2)
{
char *tmp1,*tmp2;
int pos1,pos2;
tmp1=strdup(st1);
tmp2=strdup(st2);
pos1=pos("[",st1);
pos2=pos("[",st2);
if(!(pos1||pos2))return 1;
else {
if(pos1==pos2)
{
tmp1=delete(tmp1,1,pos1);
tmp2=delete(tmp2,1,pos2);
if(pos("]",st1)&& pos("]",st2))
return 0;
else return -1;
}else return -1;
}
}
char* C2Mpitype(char *type)
{
char *temp;
if (strstr(type,"short")) {temp=Copycode(temp,"MPI_SHORT");return temp;}
if (strstr(type,"int")) {temp=Copycode(temp,"MPI_INT");return temp;}
if (strstr(type,"long")) {temp=Copycode(temp,"MPI_LONG");return temp;}
if (strstr(type,"long long")){temp=Copycode(temp,"MPI_LONG_LONG");return temp;}
if (strstr(type,"float")) {temp=Copycode(temp,"MPI_FLOAT");return temp;}
if (strstr(type,"double")) { temp=Copycode(temp,"MPI_DOUBLE");return temp;}
if (strstr(type,"Boolean")){ temp=Copycode(temp,"MPI_LOGICAL");return temp;}
if (strstr(type,"unsigned char")){temp=Copycode(temp,"MPI_UNSIGNED_CHAR");return temp;}
if (strstr(type,"TIMER")) { temp=Copycode(temp,"MPI_TIMER");return temp;}
sprintf(temp,"%s",type);return temp;
}
void yycode(char *fmt,...)
{
va_list args;
va_start(args, fmt);
vfprintf(yycodeout,fmt,args);
}
extern void YYABORT();
extern void yyerror(char *str) {
(void)fprintf(stderr,"%s\n",str);
}
int yywrap(void) {return 0;}
int TestPAR(Tree *root)
{
Tree *tmp;
if (root==NULL) return -1;
if (root->info->CType==T_PAR) return 1;
tmp=root->oldest;
while (tmp!=NULL) {
if (TestPAR(tmp)==1) return 1;
tmp=tmp->brother;
}
return 0;
}
/*-------------------------------------------------*/
int UpdateLRlist(List *varlocal,List *right,List *left)
{
Element *elem;
if (varlocal==NULL) return 0;
else {
elem=varlocal->Head;
while (elem!=NULL) {
if (FindId(right,elem->name)!=NULL)
ExtrIDname(right,elem->name);
if (FindId(left,elem->name)!=NULL)
ExtrIDname(left,elem->name);
elem=elem->Next;
}
return 1;
}
}
Tree *Seedcode(Tree *root)
{
Element *elem;
List *tt;
Tree *ptr,*rem;
char tempcode[255];
char temp[255];
if (root==NULL) return NULL;
else
if (root->oldest==NULL) return root;
else { ptr=root->oldest;
switch (root->info->CType) {
case T_SEQ : /*cap nhat lai LeftList va RightList*/
root->info->RightList=ListInit(root->info->RightList);
root->info->LeftList=ListInit(root->info->LeftList);
while (ptr!=NULL) {
if(ptr->info!=NULL){
sprintf(tempcode,"{\n%s}\n",ptr->info->code);
root->info->code=Concatcode(root->info->code,tempcode);
root->info->RightList=Union(root->info->RightList,ptr->info->RightList);
root->info->LeftList=Union(root->info->LeftList,ptr->info->LeftList);
}
rem=ptr;
ptr=ptr->brother;
FreeNode(rem);
}
if (root->info->VarLocal!=NULL) {
tempcode[0]='\0';
elem=root->info->VarLocal->Head;
while (elem!=NULL) {
sprintf(tempcode,"%s %s\t%s;\n",tempcode,elem->type,elem->name);
elem=elem->Next;
}
sprintf(temp,root->info->code);
root->info->code=Copycode(root->info->code,tempcode);
root->info->code=Concatcode(root->info->code,temp);
}
UpdateLRlist(root->info->VarLocal,root->info->RightList,root->info->LeftList);
root->oldest=NULL;
break;
case T_SEQREP:
sprintf(tempcode,"%s {\n%s}\n",root->info->code,ptr->info->code);
root->info->LeftList=Union(root->info->LeftList,ptr->info->LeftList);
root->info->RightList=Union(root->info->RightList,ptr->info->RightList);
root->info->code=Copycode(root->info->code,tempcode);
if (root->info->VarLocal!=NULL) {
tempcode[0]='\0';
elem=root->info->VarLocal->Head;
while (elem!=NULL) {
sprintf(tempcode,"%s %s\t%s;\n",tempcode,elem->type,elem->name);
elem=elem->Next;
}
sprintf(temp,root->info->code);
root->info->code=Copycode(root->info->code,tempcode);
root->info->code=Concatcode(root->info->code,temp);
}
FreeNode(ptr);
root->oldest=NULL;
break;
case T_IF:
sprintf(tempcode,"if (%s) { \n%s\n}\n"
,ptr->info->code,ptr->oldest->info->code);
root->info->code=Concatcode(root->info->code,tempcode);
ptr->info->LeftList=Union(ptr->info->LeftList,ptr->oldest->info->LeftList);
ptr->info->RightList=Union(ptr->info->RightList,ptr->oldest->info->RightList);
root->info->LeftList=Union(root->info->LeftList,ptr->info->LeftList);
root->info->RightList=Union(root->info->RightList,ptr->info->RightList);
rem=ptr;
ptr=ptr->brother;
FreeNode(rem->oldest);
FreeNode(rem);
while (ptr!=NULL) {
sprintf(tempcode,"else if (%s) {\n%s\n}\n"
,ptr->info->code,ptr->oldest->info->code);
root->info->code=Concatcode(root->info->code,tempcode);
ptr->info->LeftList=Union(ptr->info->LeftList,ptr->oldest->info->LeftList);
ptr->info->RightList=Union(ptr->info->RightList,ptr->oldest->info->RightList);
root->info->LeftList=Union(root->info->LeftList,ptr->info->LeftList);
root->info->RightList=Union(root->info->RightList,ptr->info->RightList);
rem=ptr;
ptr=ptr->brother;
FreeNode(rem->oldest);
FreeNode(rem);
}
if (root->info->VarLocal!=NULL) {
tempcode[0]='\0';
elem=root->info->VarLocal->Head;
while (elem!=NULL) {
sprintf(tempcode,"%s %s\t%s;\n",tempcode,elem->type,elem->name);
elem=elem->Next;
}
sprintf(temp,root->info->code);
root->info->code=Copycode(root->info->code,tempcode);
root->info->code=Concatcode(root->info->code,temp);
}
root->oldest=NULL;
break;
case T_IFREP:
sprintf(tempcode,"%s {\nif (%s) {\n%s}\n}\n"
,root->info->code,ptr->info->code,ptr->oldest->info->code);
root->info->code=Copycode(root->info->code,tempcode);
ptr->info->LeftList=Union(ptr->info->LeftList,ptr->oldest->info->LeftList);
ptr->info->RightList=Union(ptr->info->RightList,ptr->oldest->info->RightList);
root->info->LeftList=Union(root->info->LeftList,ptr->info->LeftList);
root->info->RightList=Union(root->info->RightList,ptr->info->RightList);
if (root->info->VarLocal!=NULL) {
tempcode[0]='\0';
elem=root->info->VarLocal->Head;
while (elem!=NULL) {
sprintf(tempcode,"%s %s\t%s;\n",tempcode,elem->type,elem->name);
elem=elem->Next;
}
sprintf(temp,root->info->code);
root->info->code=Copycode(root->info->code,tempcode);
root->info->code=Concatcode(root->info->code,temp);
}
FreeNode(ptr->oldest);
FreeNode(ptr);
break;
case T_CASE:
sprintf(temp,"%s",root->info->code);
sprintf(root->info->code,"switch (%s)\n",temp);
while ((ptr!=NULL)&&(strcmp(ptr->info->code,"else"))) {
sprintf(tempcode,"case %s:\n %sbreak;\n"
,ptr->info->code,ptr->oldest->info->code);
root->info->code=Concatcode(root->info->code,tempcode);
rem=ptr;
ptr=ptr->brother;
FreeNode(rem->oldest);
FreeNode(rem);
}
if (ptr!=NULL) {
sprintf(tempcode,"default:\n %s\n"
,ptr->oldest->info->code);
root->info->code=Concatcode(root->info->code,tempcode);
}
if (root->info->VarLocal!=NULL) {
tempcode[0]='\0';
elem=root->info->VarLocal->Head;
while (elem!=NULL) {
sprintf(tempcode,"%s %s\t%s;\n",tempcode,elem->type,elem->name);
elem=elem->Next;
}
sprintf(temp,root->info->code);
root->info->code=Copycode(root->info->code,tempcode);
root->info->code=Concatcode(root->info->code,temp);
}
root->oldest=NULL;
break;
case T_WHILE:
sprintf(tempcode,"while (%s) {\n %s }\n"
,root->info->code,ptr->info->code);
root->info->code=Copycode(root->info->code,tempcode);
if (root->info->VarLocal!=NULL) {
tempcode[0]='\0';
elem=root->info->VarLocal->Head;
while (elem!=NULL) {
sprintf(tempcode,"%s %s\t%s;\n",tempcode,elem->type,elem->name);
elem=elem->Next;
}
sprintf(temp,root->info->code);
root->info->code=Copycode(root->info->code,tempcode);
root->info->code=Concatcode(root->info->code,temp);
}
FreeNode(ptr);
root->oldest=NULL;
break;
default :
break;
}
}
return root;
}
int genseqcode(Tree *root)
{
Tree *ptr;
if (root==NULL) return 0;
else {
ptr=root->oldest;
while (ptr!=NULL) {
genseqcode(ptr);
ptr=ptr->brother;
}
if (root->info->CType==T_PAR) return -1;
else {
Seedcode(root);
}
}
return 1;
}
/*---------------------------------------------------*/
int gencode(Tree *root)
{
Tree *ptr;
if (root==NULL) return 0;
else {
if (TestPAR(root)!=1) {
genseqcode(root);
}
else {
gencode(root->brother);
gencode(root->oldest);
}
return 1;
}
}
/*--------------------------------------*/
int ghepcay(Tree *root)
{
Tree *ptr,*bro;
ptr=root->oldest;
bro=ptr->brother;
while (bro!=NULL){
ptr->brother=NULL;
while (ptr!=NULL) ptr=ptr->oldest;
bro->parent=ptr;
ptr->oldest=bro;
ptr=bro;
bro=bro->brother;
}
return 1;
}
int Seedtree(Tree *root)
{
Tree *chi,*ptr,*bro;
if (root==NULL) return 0;
else {
Seedtree(root->brother);
Seedtree(root->oldest);
if (root->info->CType!=T_SEQ) return 1;
else {
ptr=root->oldest;
if (ptr==NULL) return 1;
bro=ptr->brother;
while (bro!=NULL){
ptr->brother=NULL;
while (ptr->oldest!=NULL) ptr=ptr->oldest;
bro->parent=ptr;
ptr->oldest=bro;
ptr=bro;
bro=bro->brother;
}
}
return 1;
}
}
/*-----------------------------------------------------------*/
int IN(Tree *root)
{
Tree *ptr;
ptr=root->oldest;
while (ptr!=NULL) {
if (ptr->info->code!=NULL) printf("%s",ptr->info->code);
ptr=ptr->oldest;
}
}
/*--------------------------------------------*/
int Procnum(Tree *root)
{
int t1=0,t2=0,i=0;
Tree *ptr;
ptr=root;
if (ptr==NULL) return 0;
else {
t1=Procnum(ptr->oldest);
while(ptr!=NULL){
t2++;
ptr=ptr->brother;}
i=t1>t2 ? t1:t2;
}
return i;
}
/*-------------------------------------------------*/
int UpdateCode(Tree *root,Tree *node,int dest, char *Comm)
{
char tempcode[255];
char temp[255];
char *tmp;
Element *elem;
if ((root==NULL)||(node==NULL)) return;
if (node->info->RightList!=NULL) {
temp[0]='\0';
tempcode[0]='\0';
elem=node->info->RightList->Head;
while (elem!=NULL) {
sprintf(temp,"MPI_Send(%s,1,%s,%d,9,%s); \n"
,elem->name,C2Mpitype(elem->type),dest,Comm);
sprintf(tempcode,"MPI_Recv(%s,1,%s,0,9,%s,status);\n"
,elem->name,C2Mpitype(elem->type),Comm);
tmp=Copycode(tmp,node->info->code);
node->info->code=Copycode(node->info->code,tempcode);
node->info->code=Concatcode(node->info->code,tmp);
tmp=Copycode(tmp,root->info->code);
root->info->code=Copycode(root->info->code,temp);
root->info->code=Concatcode(root->info->code,tmp);
elem=elem->Next;
}
}
if (node->info->LeftList!=NULL) {
temp[0]='\0';
tempcode[0]='\0';
elem=node->info->LeftList->Head;
while (elem!=NULL) {
tempcode[0]='\0';
sprintf(temp,"MPI_Recv(%s,1,%s,%d,9,%s,status);\n"
,elem->name,C2Mpitype(elem->type),dest,Comm);
sprintf(tempcode,"MPI_Send(%s,1,%s,0,9,%s);\n"
,elem->name,C2Mpitype(elem->type),Comm);
root->info->code=Concatcode(root->info->code,temp);
node->info->code=Concatcode(node->info->code,tempcode);
elem=elem->Next;
}
}
}
//-------------------------------------------------*/
int SetTree(Tree *root)
{
Tree *ptr;
if(root==NULL) return 0;
else {
SetTree(root->brother);
SetTree(root->oldest);
root->info->lev=1;
return 1;
}
}
/*-------------------------------------------------*/
int GenMPI(Tree *root,char *MasterComm)
{
int i=1,j=0,k;
char Newcomm[60],
*Split,
*tmp,
*tempcode,
*tmpcode,
*temp;
Element *elem;
Tree *ptr,*older,*mark;
if (root==NULL) return -1;
else {
tempcode=(char *)malloc(255*sizeof(char));
tmpcode=(char *)malloc(255*sizeof(char));
tmp=(char *)malloc(255*sizeof(char));
temp=(char *)malloc(255*sizeof(char));
Split=(char *)malloc(255*sizeof(char));
sprintf(tempcode,"int _size,_rank;\nMPI_Status status;\n\
MPI_Comm_rank(%s,&_rank);\n",MasterComm);
root->info->code=Copycode(root->info->code,tempcode);
older=root->oldest;
ptr=older->brother;
while (ptr!=NULL) {
j++;
k=i;
i+=ptr->info->lev;
sprintf(Newcomm,"%s_%d",MasterComm,j);
if (ptr->info->RightList!=NULL) {
elem=ptr->info->RightList->Head;
while (elem!=NULL) {
sprintf(temp,"MPI_Send(%s,1,%s,%d,9,%s); \n"
,elem->name,C2Mpitype(elem->type),j,MasterComm);
sprintf(tempcode,"MPI_Recv(%s,1,%s,0,9,%s,status);\n"
,elem->name,C2Mpitype(elem->type),MasterComm);
tmp=Copycode(tmp,ptr->info->code);
ptr->info->code=Copycode(ptr->info->code,tempcode);
ptr->info->code=Concatcode(ptr->info->code,tmp);
tmp=Copycode(tmp,older->info->code);
older->info->code=Copycode(older->info->code,temp);
older->info->code=Concatcode(older->info->code,tmp);
elem=elem->Next;
}
}
if (ptr->info->LeftList!=NULL) {
elem=ptr->info->LeftList->Head;
while (elem!=NULL) {
sprintf(temp,"MPI_Recv(%s,1,%s,%d,9,%s,status);\n"
,elem->name,C2Mpitype(elem->type),j,MasterComm);
sprintf(tempcode,"MPI_Send(%s,1,%s,0,9,%s);\n"
,elem->name,C2Mpitype(elem->type),MasterComm);
older->info->code=Concatcode(older->info->code,temp);
ptr->info->code=Concatcode(ptr->info->code,tempcode);
elem=elem->Next;
}
}
sprintf(tmp,"MPI_Comm_split(%s,((%d<=_rank)&&(_rank<%d)),0,%s);\n",MasterComm,k,i,Newcomm);
Split=Concatcode(Split,tmp);
sprintf(tmp,"if((_rank>=%d)&&(_rank < %d)){\n\
MPI_Barrier(%s);\n%s\nMPI_Barrier(%s);\n}\n",k,i,MasterComm,ptr->info->code,MasterComm);
tmpcode=Concatcode(tmpcode,tmp);
ptr=ptr->brother;
}
tmp=Copycode(tmp,Split);
sprintf(Newcomm,"%s_0",MasterComm);
sprintf(Split,
"MPI_Comm_split(%s,((0<=_rank)&&(_rank<1)),0,%s);\n",MasterComm,Newcomm);
Split=Concatcode(Split,tmp);
tmp=Copycode(tmp,tmpcode);
sprintf(temp,"\0");
ptr=root->oldest->oldest;
while (ptr!=NULL) {
temp=Concatcode(temp,ptr->info->code);
ptr=ptr->oldest;
}
ptr=root->oldest->oldest;
while (ptr!=NULL) {
mark=ptr;
ptr=ptr->oldest;
FreeNode(mark);
}
sprintf(tmpcode,"if((_rank>=0)&&(_rank < 1)){\nMPI_Barrier(%s);\n%s\n\
MPI_Barrier(%s);%s\n}\n",MasterComm,older->info->code,MasterComm,temp);
tmpcode=Concatcode(tmpcode,tmp);
root->info->lev=i;
root->info->code=Concatcode(root->info->code,Split);
root->info->code=Concatcode(root->info->code,tmpcode);
return 1;
}
}
/*--------------------------------------------------*/
int GenParCode(Tree *root,char *Comm,int index)
{
int i=0,j=0;
char rr[50];
char Newcomm[50];
char *temp;
Tree *ptr,*tr,*child;
if (root==NULL) return 0;
else{
child=root->oldest;
sprintf(Newcomm,"%s_%d",Comm,index);
GenParCode(root->oldest,Newcomm,0);
GenParCode(root->brother,Newcomm,index+1);
if (root->info->CType==T_PAR) {
GenMPI(root,Comm);
ptr=root->oldest;
child=ptr->oldest;
root->oldest=child;
if (child!=NULL) child->parent=root;
while (ptr!=NULL) {
tr=ptr;
ptr=tr->brother;
FreeNode(tr);
}
}
if (root->info->CType==T_SEQ) {
sprintf(temp,"\0");
ptr=root->oldest;
while (ptr!=NULL) {
temp=Concatcode(temp,ptr->info->code);
ptr=ptr->oldest;
}
ptr=root->oldest->oldest;
while (ptr!=NULL) {
tr=ptr;
ptr=ptr->oldest;
FreeNode(tr);
}
root->info->code=Copycode(root->info->code,temp);
}
return 1;
}
}
/*-------------------------------------*/
int main()
{
int a;
Tree *po;
FILE *codeout;
yycodeout=fopen("code.out","w");
a=yyparse();
Seedcode(root);
a=gencode(root);
Seedtree(root);
SetTree(root);
GenParCode(root,"MPI_Global",0);
codeout=fopen("temp.out","w+");
po=root->oldest->oldest;
while(po!=NULL) {
fprintf(codeout,po->info->code);
po=po->oldest;
}
fclose(codeout);
Print_tree(root,0);
fclose(yycodeout);
Print_tree(root,0);
}
Maõ nguoàn cuûa caùc file hoã trôï:
File :chan.h
#include
#include
#include
#include "str.h"
#ifndef REQ_SEND
#define REQ_SEND 0
#endif
#ifndef REQ_RECV
#define REQ_RECV 1
#endif
#ifndef REQ_RECV_ALT
#define REQ_RECV_ALT 2
#endif
#ifndef REQ_ACCEPT
#define REQ_ACCEPT 3
#endif
#ifndef REQ_EXIT
#define REQ_EXIT 4
#endif
#ifndef REQ_DESTROY
#define REQ_DESTROY 5
#endif
int Chanindex=0;
typedef struct Chan_node {
char name[255];
int send_id;
int recv_id;
char index[255];
struct Chan_node *Prev;
struct Chan_node *Next;
}Chan_node;
typedef struct Chan_list {
int Count;
Chan_node *Head;
Chan_node *Tail;
}Chan_list;
/************End of declaration data structure*******/
Chan_list *Init_chan_list(Chan_list *chan_list)
{
chan_list=(Chan_list *)malloc(sizeof(Chan_list));
if(chan_list!=NULL) {
chan_list->Count=0;
chan_list->Head=NULL;
chan_list->Tail=NULL;
return chan_list;
}
return NULL;
}
Chan_node *Set_chan_node(char *name,char *index)
{
Chan_node *temp;
temp=(Chan_node *)malloc(sizeof(Chan_node));
if (temp==NULL) return temp;
temp->send_id=-1;
temp->recv_id=-1;
temp->Prev=NULL;
temp->Next=NULL;
sprintf(temp->name,"%s",name);
sprintf(temp->index,"%s",index);
return temp;
}
Chan_node *Add_chan(Chan_list *chan_list,Chan_node *chan_node)
{
Chan_node *temp;
if(chan_list==NULL) return NULL;
temp=(Chan_node *)malloc(sizeof(Chan_node));
if(temp==NULL) return temp;
*temp=*chan_node;
if(chan_list->Count==0) {
chan_list->Head=temp;
chan_list->Tail=temp;
chan_list->Count++;
return temp;
}
temp->Prev=chan_list->Tail;
chan_list->Tail->Next=temp;
chan_list->Tail=temp;
chan_list->Count++;
return temp;
}
Chan_node *Find_chan(Chan_list *chan_list,char *name)
{
Chan_node *temp;
if(chan_list==NULL) return NULL;
temp=chan_list->Head;
while(temp!=NULL) {
if(strcmp(temp->name,name)==0) return temp;
temp=temp->Next;
}
return temp;
}
Chan_node *Find_index(Chan_list *chan_list,char *index)
{
Chan_node *temp;
if(chan_list==NULL) return NULL;
temp=chan_list->Head;
while(temp!=NULL) {
if(strcmp(temp->index,index)==0) return temp;
temp=temp->Next;
}
return temp;
}
int Delete_chan_node(Chan_list *chan_list,char *chan_name)
{
Chan_node *temp;
if ((temp = Find_chan(chan_list,chan_name))==NULL)
printf("Channel is not available\n");
else if (chan_list->Count==1) {
chan_list->Head=NULL;
chan_list->Tail=NULL;
chan_list->Count--;
free(temp);
}
else {
if (temp->Prev==NULL) {
chan_list->Head=temp->Next;
temp->Next->Prev=NULL;
chan_list->Count--;
free(temp);
}
else if (temp->Next==NULL) {
chan_list->Tail=temp->Prev;
temp->Prev->Next=NULL;
chan_list->Count--;
free(temp);
}
else {
temp->Prev->Next=temp->Next;
temp->Next->Prev=temp->Prev;
chan_list->Count--;
free(temp);
}
}
}
void Release_chan_list(Chan_list *chan_list)
{
Chan_node *temp,*before;
if(chan_list!=NULL) {
temp=chan_list->Head;
while(temp!=NULL) {
before=temp;
temp=temp->Next;
chan_list->Head=temp;
free(before);
chan_list->Count--;
}
chan_list->Tail=NULL;
chan_list->Head=NULL;
}
}
void Release_temp_chan_list(Chan_list *chan_list)
{
Chan_node *temp,*before;
if(chan_list!=NULL) {
temp=chan_list->Head;
while(temp) {
before=temp;
temp=temp->Next;
chan_list->Head=temp;
free(before);
chan_list->Count--;
}
chan_list->Tail=NULL;
chan_list->Head=NULL;
}
}
Chan_list *Copy_chan_list(Chan_list *chan_list1,Chan_list chan_list2) {
Chan_node *temp,*p;
chan_list1=Init_chan_list(chan_list1);
if(chan_list1==NULL) return NULL;
chan_list1->Count=0;
p=chan_list2.Head;
while(p!=NULL) {
*temp=*p;
if(Add_chan(chan_list1,temp)==NULL) {
Release_chan_list(chan_list1);
return NULL;
}
p=p->Next;
}
return chan_list1;
}
int Set_chan_request(Chan_list *list,char *chan_name,int request,int id)
{
Chan_node *chan,*index;
char *rr;
int i,ll;
i=pos("_",chan_name);
if(i!=0){
rr=strdup(chan_name);
rr=delete(rr,1,i);
ll=strlen(chan_name);
chan_name=delete(chan_name,i,ll-1);
}
else rr=NULL;
chan =Find_chan(list,chan_name);
index=Find_index(list,rr);
if (chan==NULL) {
printf("Set up chan: %s\n",chan_name);
if((rr==NULL)||((index==NULL)&&(rr!=NULL))){
chan=Set_chan_node(chan_name,rr);
if ((chan=Add_chan(list,chan))==NULL) {
printf("Can't add channel\n");
return -1;}
}
}
if (request==REQ_SEND) {
if (chan->send_id==-1) chan->send_id=id;
else {
printf("Channel collision!");
return -1;
}
}
else if (request==REQ_RECV) {
if (chan->recv_id==-1) chan->recv_id=id;
else {
printf("Channel collision!");
return -1;
}
}
else {
printf("Request unknown!");
return 0;
}
return 1;
}
int Test_chan(Chan_list *list)
{
Chan_node *temp;
temp=list->Head;
while (temp!=NULL) {
if ((temp->send_id!=-1)&&(temp->recv_id!=-1)) {
MPI_Send(&(temp->send_id), 1, MPI_INT,temp->recv_id, REQ_ACCEPT, MPI_COMM_WORLD);
MPI_Send(&(temp->recv_id), 1, MPI_INT,temp->send_id, REQ_ACCEPT, MPI_COMM_WORLD);
Delete_chan_node(list,temp->name);
}
if ((temp->recv_id!=-1)&&(Find_index(list,temp->index)!=NULL)) {
MPI_Send(&(temp->send_id), 1, MPI_INT,temp->recv_id,REQ_DESTROY, MPI_COMM_WORLD);
Delete_chan_node(list,temp->name); }
temp=temp->Next;
}
return;
}
/*-------------------------ham quan ly kenh -------------------------------*/
int Chan_master(int size)
{
int child;
int i;
char *rr,*temp;
char *chan_name;
MPI_Status status;
Chan_list *list;
chan_name=(char *)malloc(255*sizeof(char));
child=size-1;
list = Init_chan_list(list);
while (child) {
MPI_Recv(chan_name, 10, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG,MPI_COMM_WORLD, &status);
switch (status.MPI_TAG) {
case REQ_SEND :
Set_chan_request(list,chan_name,REQ_SEND,status.MPI_SOURCE);
Test_chan(list);
continue;
case REQ_RECV :
Set_chan_request(list,chan_name,REQ_RECV,status.MPI_SOURCE);
Test_chan(list);
continue;
case REQ_RECV_ALT:
Set_chan_request(list,chan_name,REQ_RECV,status.MPI_SOURCE);
Test_chan(list);
continue;
case REQ_EXIT :
child--;
continue;
default : printf("Unknown message!");
}
}
}
/*------------------------------- Yeu cau goi tren kenh----------------------*/
int Chan_client_send(char *Chan_name)
{
int id;
MPI_Status status;
MPI_Send( Chan_name,strlen(Chan_name), MPI_CHAR, 0, REQ_SEND, MPI_COMM_WORLD );
MPI_Recv( &id, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status );
if (status.MPI_TAG==REQ_ACCEPT) return id;
return -1;//include DESTROY
}
/*------------------------------- Yeu cau nhan tren kenh----------------------*/
int Chan_client_recv(char *Chan_name)
{
int id;
MPI_Status status;
MPI_Send( Chan_name,strlen(Chan_name), MPI_CHAR, 0, REQ_RECV, MPI_COMM_WORLD );
MPI_Recv( &id, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status );
if (status.MPI_TAG==REQ_ACCEPT) return id;
return -1;//include DESTROY
}
/*------------------------------- Yeu cau nhan tren kenh co ALT----------------------*/
int Chan_client_alt_recv(char *Chan_name)
{
int id;
MPI_Status status;
MPI_Send( Chan_name,strlen(Chan_name), MPI_CHAR, 0, REQ_RECV_ALT, MPI_COMM_WORLD );
MPI_Recv( &id, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status );
if (status.MPI_TAG==REQ_ACCEPT) return id;
return -1;//include DESTROY
}
/*------------------------------- Huy kenh ----------------------*/
int Chan_client_exit()
{
MPI_Send( "" , 1, MPI_CHAR, 0, REQ_EXIT, MPI_COMM_WORLD );
return 1;
}
File :List.H
#include
#include "tool.h"
#include "str.h"
typedef struct Element {
char *name;
char *type;
int Lev;
struct Element *Prev;
struct Element *Next;
}Element;
typedef struct List {
int Count;
Element *Head;
Element *Tail;
}List;
/****KHAI BAO CAU TRUC CHO KENH TRUEYN VA PROTOCOL*******/
typedef struct CElement {
char name[255];
char type[255];
struct List *Link;
struct CElement *Prev;
struct CElement *Next;
}CElement;
typedef struct Chanlist {
int Count;
CElement *Head;
CElement *Tail;
} Chanlist;
/*******************************/
List *ListInit(List *list)
{
list=(List *)malloc(sizeof(List));
if(list!=NULL) {
list->Count=0;
list->Head=NULL;
list->Tail=NULL;
return list;
}
return NULL;
}
Chanlist *ChanListInit(Chanlist *list)
{
list=(Chanlist *)malloc(sizeof(Chanlist));
if(list!=NULL) {
list->Count=0;
list->Head=NULL;
list->Tail=NULL;
return list;
}
return NULL;
}
Element *SetElement(char *name,char *type,int lev)
{
Element *temp;
temp=(Element *)malloc(sizeof(Element));
if (temp==NULL) return temp;
temp->Prev=NULL;
temp->Next=NULL;
temp->Lev=lev;
temp->name=Copycode(temp->name,name);
temp->type=Copycode(temp->type,type);
return temp;
}
CElement *SetCElement(char *name,char *type,List *pro)
{
CElement *temp;
temp=(CElement *)malloc(sizeof(CElement));
if (temp==NULL) return temp;
temp->Prev=NULL;
temp->Next=NULL;
sprintf(temp->name,name);
sprintf(temp->type,type);
temp->Link=pro;
return temp;
}
Element *AddElement(List *list,char *name,char *type,int lev) {
Element *temp;
if(list==NULL) return NULL;
temp=(Element *)malloc(sizeof(Element));
if(temp==NULL) return temp;
temp=SetElement(name,type,lev);
if(list->Count==0) {
list->Head=temp;
list->Tail=temp;
list->Count++;
return temp;
}
temp->Prev=list->Tail;
list->Tail->Next=temp;
list->Tail=temp;
list->Count++;
return temp;
}
CElement *AddCElement(Chanlist *list,char *name,char *type, List *pro) {
CElement *temp;
if(list==NULL) return NULL;
temp=(CElement *)malloc(sizeof(CElement *));
if(temp==NULL) return temp;
temp=SetCElement(name,type,pro);
if(list->Count==0) {
list->Head=temp;
list->Tail=temp;
list->Count++;
return temp;
}
temp->Prev=list->Tail;
list->Tail->Next=temp;
list->Tail=temp;
list->Count++;
return temp;
}
Element *FindElementtag(Chanlist *list,char *name)
{
Element *temp;
if(list==NULL) return NULL;
temp=list->Head->Link->Head;
while(temp!=NULL) {
if(!strcmp(temp->name,name))
return temp;
temp=temp->Next;
}
return temp;
}
CElement *FindCElement(Chanlist *list,char *name)
{
CElement *temp;
if(list==NULL) return NULL;
temp=list->Head;
while(temp!=NULL) {
if(!strcmp(temp->name,name))
return temp;
temp=temp->Next;
}
return temp;
}
Element *FindElement(List *list,char *name,char *type,int lev)
{
Element *temp;
if(list==NULL) return NULL;
temp=list->Head;
while(temp!=NULL) {
if((!strcmp(temp->name,name))&&(!strcmp(temp->type,type))&&(temp->Lev==lev))
return temp;
temp=temp->Next;
}
return temp;
}
Element *FindId(List *list,char *name)
{
Element *temp;
if(list==NULL) return NULL;
temp=list->Head;
while(temp!=NULL) {
if(!Checkarr(temp->name,name))
return temp;
else { if(!strcmp(temp->name,name))
return temp;
}
temp=temp->Next;
}
return temp;
}
int DeleteElement(List *list,char *name,char *type,int lev)
{
Element *temp;
if ((temp = FindElement(list,name,type,lev))==NULL)
{
printf("List is not available\n");
return 0;
}
if (list->Count==1) {
list->Head=NULL;
list->Tail=NULL;
list->Count--;
free(temp);
}
else {
if (temp->Prev==NULL) {
list->Head=temp->Next;
temp->Next->Prev=NULL;
list->Count--;
free(temp);
}
else if (temp->Next==NULL) {
list->Tail=temp->Prev;
temp->Prev->Next=NULL;
list->Count--;
free(temp);
}
else {
temp->Prev->Next=temp->Next;
temp->Next->Prev=temp->Prev;
list->Count--;
free(temp);
}
}
return 1;
}
List *DelElement(List *list,Element *elem)
{
Element *ele;
ele=elem;
if (ele==NULL) return list;
if (list==NULL) return NULL;
if (list->Head==NULL) return list;
if (list->Count==1) {
list->Head=NULL;
list->Tail=NULL;
list->Count--;
free(ele);
}
else {
if (ele->Prev==NULL) {
list->Head=ele->Next;
ele->Next->Prev=NULL;
list->Count--;
free(ele);
}
else if (ele->Next==NULL) {
list->Tail=ele->Prev;
ele->Prev->Next=NULL;
list->Count--;
free(ele);
}
else {
ele->Prev->Next=ele->Next;
ele->Next->Prev=ele->Prev;
list->Count--;
free(ele);
}
}
return list;
}
List *Release_list(List *list) {
Element *temp,*before;
if(list==NULL) return NULL;
temp=list->Head;
while(temp!=NULL) {
before=temp;
temp=temp->Next;
list->Head=temp;
free(before);
list->Count--;
}
list->Tail=NULL;
list->Head=NULL;
return list;
}
void Release_temp_list(List *list) {
Element *temp,*before;
if(list!=NULL) {
temp=list->Head;
while(temp) {
before=temp;
temp=temp->Next;
list->Head=temp;
temp->Prev=NULL;
free(before);
list->Count--;
}
list->Tail=NULL;
list->Head=NULL;
}
}
List *CopyList(List *src)
{
List *temp;
Element *elem;
temp=ListInit(temp);
if (temp==NULL) return NULL;
if (src==NULL) {temp=NULL;return NULL;}
else{ elem=src->Head;
while (elem!=NULL) {
AddElement(temp,elem->name,elem->type,elem->Lev);
elem=elem->Next;
}
return temp;
}
}
Chanlist *CopyChanList(Chanlist *src)
{
Chanlist *temp;
CElement *elem;
Element *tmp;
temp=ChanListInit(temp);
if (temp==NULL) return NULL;
if (src==NULL) return NULL;
elem=src->Head;
while (elem!=NULL) {
AddCElement(temp,elem->name,elem->type,elem->Link);
elem=elem->Next;
}
return temp;
}
int ViewList(List *src)
{
Element *elem;
if (src==NULL) return 0;
if (src->Head==NULL) return 0;
elem=src->Head;
while (elem!=NULL) {
printf("%s\t%s\n",elem->name,elem->type);
elem=elem->Next;
}
return 0;
}
int ViewChanList(Chanlist *src)
{
CElement *elem;
Element *tmp;
if (src==NULL) return 0;
elem=src->Head;
while (elem!=NULL) {
printf("%s\t%s\t",elem->name,elem->type);
if(elem->Link!=NULL){
tmp=elem->Link->Head;
while(tmp!=NULL){
printf("%s\t%s\n",tmp->name,tmp->type);
tmp=tmp->Next;}
}
elem=elem->Next;
}
return 0;
}
int CountList(List *src)
{
int i=0;
Element *elem;
if (src==NULL) return 0;
elem=src->Head;
while (elem!=NULL) {
i++;
elem=elem->Next;
}
return i;
}
List *Insert(List *list1,List *list2)
{
Element *elem;
if (list1==NULL) return list2;
else{
elem=list1->Head;
while (elem!=NULL) {
AddElement(list2,elem->name,elem->type,elem->Lev);
elem=elem->Next;
}
}
return list2;
}
List *ExtractID(List *list,int lev)
{
Element *ele,*bag;
if (list==NULL) return NULL;
if (list->Head==NULL) return NULL;
ele=list->Head;
while (ele!=NULL){
if (ele->Lev==lev){
if (list->Count==1) {
list->Head=NULL;
list->Tail=NULL;
list->Count--;
}
else {
if (ele->Prev==NULL) {
list->Head=ele->Next;
ele->Next->Prev=NULL;
list->Count--;
}
else if (ele->Next==NULL) {
list->Tail=ele->Prev;
ele->Prev->Next=NULL;
list->Count--;
}
else {
ele->Prev->Next=ele->Next;
ele->Next->Prev=ele->Prev;
list->Count--;
}
}
bag=ele;
ele=ele->Next;
free(bag);
}
else ele=ele->Next;
}
return list;
}
/*---------------------------------------------------*/
int ExtrIDname(List *list,char *name)
{
Element *ele,*bag;
if (list==NULL) return 0;
if (list->Head==NULL) return 0;
ele=list->Head;
while (ele!=NULL){
if (!strcmp(ele->name,name)){
if (list->Count==1) {
list->Head=NULL;
list->Tail=NULL;
list->Count--;
}
else {
if (ele->Prev==NULL) {
list->Head=ele->Next;
ele->Next->Prev=NULL;
list->Count--;
}
else if (ele->Next==NULL) {
list->Tail=ele->Prev;
ele->Prev->Next=NULL;
list->Count--;
}
else {
ele->Prev->Next=ele->Next;
ele->Next->Prev=ele->Prev;
list->Count--;
}
}
bag=ele;
ele=ele->Next;
free(bag);
}
else ele=ele->Next;
}
return 1;
}
/*---------------------------------------------------*/
Chanlist *InsertChan(Chanlist *list1,Chanlist *list2)
{
Chanlist *temp;
if (list1==NULL) return list2;
temp=(Chanlist *)malloc(sizeof(Chanlist));
if (temp==NULL) return NULL;
temp=CopyChanList(list1);
if (list2==NULL) return temp;
if (list2->Head==NULL) return temp;
temp->Tail->Next=list2->Head;
list2->Head->Prev=temp->Tail;
list2->Count+=list1->Count;
list2->Head=temp->Head;
free(temp);
return list2;
}
Chanlist *Release_Chanlist(Chanlist *list) {
CElement *temp,*before;
if(list==NULL) return NULL;
temp=list->Head;
while(temp!=NULL) {
before=temp;
temp=temp->Next;
list->Head=temp;
free(before);
list->Count--;
}
list->Tail=NULL;
list->Head=NULL;
return list;
}
List *Benlock(List *list1,List *list2)
{
List *list;
Element *temp;
if ((list1==NULL)||(list2==NULL)) return NULL;
if ((list1->Head==NULL)||(list2->Head==NULL)) return NULL;
list=ListInit(list);
temp=list1->Head;
while (temp!=NULL) {
if (FindElement(list2,temp->name,temp->type,temp->Lev)!=NULL) {
if (AddElement(list,temp->name,temp->type,temp->Lev)==NULL)
printf("Can't Benlock\n");
}
temp=temp->Next;
}
if (list->Head==NULL) return NULL;
else return list;
}
List *Union(List *list1,List *list2)
{
List *list;
Element *temp,*tp;
if ((list1==NULL)&&(list2==NULL)) return NULL;
else {
list=ListInit(list);
if(list1!=NULL)
list=CopyList(list1);
temp=list2->Head;
while (temp!=NULL) {
if (FindElement(list,temp->name,temp->type,temp->Lev)==NULL) {
if (AddElement(list,temp->name,temp->type,temp->Lev)==NULL)
printf("Can't Bemlock\n");
}
temp=temp->Next;
}
if (list->Head==NULL) return NULL;
else return list;
}
}
int Checkarr(char *st1, char *st2)
{
char *tmp1,*tmp2;
int pos1,pos2;
tmp1=strdup(st1);
tmp2=strdup(st2);
pos1=pos("[",st1);
pos2=pos("[",st2);
if(!(pos1||pos2))return 1;//khong phai la kieu mang
else {
if(pos1==pos2)
{
if(!strncmp(tmp1,tmp2,pos1)){
tmp1= delete(tmp1,1,pos1);
tmp2= delete(tmp2,1,pos2);
if(pos("]",tmp1)&& pos("]",tmp2))
return 0; //bien dang xet thoa mang la bien mang
else return 1;//bien mang khong dung
}
}else return 1;
}
}
File:Str.H
#include
#include
#include
#include
unsigned char pos(char *subst, char *st)
{
char *s;
s = strstr(st, subst);
if (s) return (s - st + 1);
else return 0;
}
char *delete(char *st, char pos, char num)
{
char k;
char *supp;
supp = strdup(st);
for (k = pos - 1; supp[k + num]; k++)
supp[k] = supp[k + num];
supp[k] = '\0';
return (supp);
}
char *del(char *st, int num)
{ char *temp;
int i,l=strlen(st);
temp=(char *)malloc(sizeof(char *));
for(i=0;i<num;i++)temp[i]=st[i];
temp[num-1]='\0';
for (i=num;i<=l;i++)st[i-num]=st[i];
st[l-num] = '\0';
return temp;
}
File: Tool.H
#ifndef _TOOL_H_
#define _TOOL_H_
#include
#include
#include
#include
#include
char *Concatcode(char *Codestring,char *code) {
int n,i=1;
char *tcode;
if(code==NULL) return NULL;
if(Codestring!=NULL) {
i=strlen(Codestring);
tcode=(char *)malloc((i+1)*sizeof(char));
strcpy(tcode,Codestring);
n=i+strlen(code);
Codestring=(char *)malloc((n+1)*sizeof(char));
strcpy(Codestring,tcode);
strcat(Codestring,code);
return Codestring;
}
else {
Codestring=(char *)malloc((strlen(code)+1)*sizeof(char));
strcpy(Codestring,code);
return Codestring;
}
}
char *Copycode(char *dest,char *source) {
if(source==NULL) return NULL;
dest=(char *)malloc((strlen(source)+1)*sizeof(char));
if(dest==NULL) return NULL;
sprintf(dest,"%s",source);
return dest;
}
#endif
file:tree.h
/******Data structure for Tree manipulation********/
#include
#include
#include
#include "list.h"
#include "tool.h"
/**************************/
typedef struct Info {
int lev;
int CType;
char *code;
struct List *LeftList;
struct List *RightList;
struct List *VarLocal;
struct List *ChanLocal;
struct List *ProLocal;
int ChildCount;
}Info;
typedef struct Tree {
struct Info *info;
struct Tree *parent,*oldest,*brother;
} Tree;
/**************************/
Info *Setinfo(int Lev,int CType,char *code,List *LeftList,List *RightList,
List *VarLocal,List *ChanLocal,List *ProLocal)
{
Info *temp;
temp=(Info *)malloc(sizeof(Info));
temp->lev=Lev;
temp->CType=CType;
temp->code=Copycode(temp->code,code);
temp->LeftList =CopyList(LeftList);
temp->RightList=CopyList(RightList);
temp->VarLocal =CopyList(VarLocal);
temp->ChanLocal=CopyList(ChanLocal);
temp->ProLocal=CopyList(ProLocal);
return temp;
}
/**************************/
Tree *Tree_init(Tree *tr)
{
Tree *temp;
temp=(Tree*)malloc(sizeof(Tree));
temp->info =NULL;
temp->parent =NULL;
temp->oldest =NULL;
temp->brother =NULL;
tr = temp;
return tr;
}
/*************************/
Tree *Make_child_info(Tree *tr,Info *info)
{
Tree *temp;
temp=(struct Tree *)malloc(sizeof(struct Tree));
if(temp==NULL) return NULL;
temp->parent =tr;
temp->oldest =NULL;
temp->brother =NULL;
temp->info =info;
tr->oldest =temp;
tr = tr->oldest;
return tr;
}
/*************************/
Tree *Make_child(Tree *tr)
{
Tree *temp;
temp=(struct Tree *)malloc(sizeof(struct Tree));
if(temp==NULL) return NULL;
temp->parent =tr;
temp->oldest =NULL;
temp->brother =NULL;
temp->info =Setinfo(0,0,NULL,NULL,NULL,NULL,NULL,NULL);
tr->oldest =temp;
tr = tr->oldest;
return tr;
}
/*************************/
Tree *Make_brother_info(Tree *tr,struct Info *info)
{
Tree *temp;
temp=(struct Tree *)malloc(sizeof(struct Tree));
if(temp==NULL) return NULL;
temp->parent=tr->parent;
temp->oldest=NULL;
temp->brother=NULL;
temp->info=info;
tr->brother=temp;
tr=tr->brother;
return tr;
}
/*************************/
Tree *Make_brother(Tree *tr)
{
Tree *temp;
temp=(struct Tree *)malloc(sizeof(struct Tree));
if(temp==NULL) return NULL;
temp->parent=tr->parent;
temp->oldest=NULL;
temp->brother=NULL;
temp->info=Setinfo(0,0,"",NULL,NULL,NULL,NULL,NULL);
tr->brother=temp;
tr=tr->brother;
return tr;
}
/*************************/
Tree *Add_tree(Tree *tr)
{
Tree *trace;
trace=tr;
if (trace->oldest!=NULL) {
trace=trace->oldest;
while (trace->brother!=NULL) {
trace=trace->brother;
}
trace=Make_brother(trace);
}
else trace=Make_child(trace);
return trace;
}
/*************************/
Tree *Walk_back(Tree *tr)
{
tr=tr->parent;
return tr;
}
/*************************/
int Total_node(Tree *tr)
{
if (tr == NULL)
return 0;
else
return Total_node(tr->oldest) + Total_node(tr->brother) + 1;
}
/****************************/
int Deg_tree(Tree *tr)
{
int t1,t2;
if(tr==NULL) return 0;
else {
t1=Deg_tree(tr->brother);
t2=Deg_tree(tr->oldest)+1;
return (t1 >=t2 ?t1:t2);
}
}
/****************************/
Tree *Find_node(Tree *tr,struct Info *info)
{
Tree *tmp;
if(tr==NULL) return NULL;
if(tr->info==info) return tr;
if ((tmp=Find_node(tr->oldest,info))!=NULL) return tmp;
return Find_node(tr->brother,info);
}
int FreeNode(Tree *node)
{
if (node==NULL) return 0;
else{
Release_list(node->info->LeftList);
free(node->info->LeftList);
Release_list(node->info->RightList);
free(node->info->RightList);
Release_list(node->info->VarLocal);
free(node->info->VarLocal);
Release_list(node->info->ChanLocal);
free(node->info->ChanLocal);
Release_list(node->info->ProLocal);
free(node->info->ProLocal);
node->info->code=(char *)malloc(sizeof(char));
free(node->info->code);
free(node->info);
free(node);
return 1;
}
}
/****************************/
Tree *Free_tree(Tree *tr)
{
Tree *tmp;
tmp=tr;
if (tr==NULL) return tr;
if (tr->brother==tr->oldest) {
free(tr);
return NULL;
}
else {
Free_tree(tr->brother);
Free_tree(tr->oldest);
free(tr);
return NULL;
}
}
/*************************/
void Print_tree(Tree *tr, int m)
{
int i;
for (i = 0; i < m; i++) printf("-");
if (tr != NULL)
{
printf("%d\n", tr->info->lev);
printf("%d\n", tr->info->CType);
Print_tree(tr->oldest,m+1);
Print_tree(tr->brother,m);
}
else printf("\n");
}
PHUÏ LUÏC A
Language.lex
(scaner description)
lex
lex.yy.c
(scanner)
Other
libaries & object
Source code
Language.y
(parse description)
yacc
y.tab.c
yyparse( )
y.tab.h
(token)
C compiler
linker
Llib
y.output
(State machine )
yyout.sym
(Symbol table)
Generated
Compiler
Translate
code
Hình A. Taïo Compiler duøng LEX vaø YACC
-v
-d -s
cc
ld
Phuï luïc B
Caùc luaät sinh cuûa ngoân ngöõ OCCAM
1. Process:
Process = SKIP | STOP
| action
| contrustion
| instance
Action = Assignment | input | output
Assignment = variable := expression
Input = channel ? variable
Output = channel ! expression
Assignment = variable.list := exprestion.list
Variable.list = { 1, variable }
Exprestion.list = { 1, expression}
2. Contrustion:
Contruction = sequence | conditional | selection | loop
| parallel | alternation
Sequence = SEQ { Process }
Conditional = IF { choice }
Choice = guarded.choice | conditional
Guarded.choice = boolean Process
Boolean = expression
Selection = CASE selector { option }
Option = { 1, case. expression }
Process
| ELSE Process
Selector = expression
Case. Expression = expression
Loop = WHILE boolean
Process
Parallel = PAR { Process }
Alternation = ATL { alternative}
Alternative = guarded. alternative | alternation
Guarded. alternative = guard
{ Process }
Guard = input
| boolean & input
| boolean & SKIP
3. Replication:
Sequence = SEQ replicator
Process
Conditional = IF replicator
choice
Parallel = PAR replicator
Process
Alternation = ATL replicator
alternative
Replicator = name = base For count
Base = expression
Count = expression
4.Types:
Type = Primitive.Type
| Array.Type
Primitive.Type = CHAN OF Protocol
| TIMER
| BOOL
| BYTE
| INT
| INT16
| INT32
| INT64
| REAL32
| REAL64
Array.Type = [expression ] type
5. Literal :
Literal = integer
| byte
| integer (type)
| byte ( type)
| real (type)
| string
| TRUE | FALSE
Integer = digits | #hex.digits
Byte = ‘Character’
Real = digits. digits | digits. digits | exponent
Exponent = + digits | - digits
Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Hex.digits = digit | A | B | C | D | E | F
6. Declaration:
Declaration = type {1, name}
7. Protocol:
Definition = PROTOCOL name IS simple.protocol
| PROTOCOL name IS sequential.protocol
Protocol = name
Simple.protocol = type
| Primitive.Type :: [] type
Input = channel ? input.item
Input.item = variable
| variable :: variable
Output = channel ! output.item
Output.item = expression
| expression :: expression
Protocol = simple.protocol
Sequential.protocol = { 1, simple.protocol }
Input = channel ? { 1, input.item }
Output = channel ! { 1, output.item}
Definition = PROTOCOL name
CASE {tagged. protocol }
Tagged. protocol = tag
| tag ; sequential.protocol
Tag = name
Output = channel ! tag
| channel ! tag ; { 1, output.item }
Case.input = channel ? CASE
{ variant }
Variant = tagged.list
Protocol
| specification
variant
Tagged.list = tag
= | tag ; { 1, input.item }
Process = case.input
Input = channel ? CASE tagged.list
Alternative = channel ? CASE
{ variant }
| boolean & channel ? CASE
{ variant }
8. Timer Access:
Input = timer.input
| delayed.input
Timer.input = timer ? variable
Delayed.input = timer ? AFTER expression
9. Element:
Element = element [ subscript]
| [ element FROM subscript FOR count ]
| name
Subscript = expression
Variable = element
Channel = element
Timer = element
10. Expression:
Operant = element
| literal
| table
| (expression )
Expression = monadic.operator operant
| operant. dyadic.operator operant
| conversion
| operant
Table = table [subscript ]
| [ { 1, expression } ]
| [ table FROM subscript FOR count ]
Expression = MOSTPOS type
| MOSTNEG type
Conversion = Primitive.type operant
| Primitive.type ROUND operant
| Primitive.type TRUNC operant
11. Abbreviation :
Abbreviation = specifier name IS element
| name IS element
| VAL specifier name IS expression
| VAL name IS expression
Specifier = Primitive.type
| [] specifier
| [expression ] specifier
12. Scope :
Process = specification
Process
Choice = specification
choice
Option = specification
option
Alternative = specification
alternative
Variant = specification
variant Valof = specification
valof
Specification = declaration | abbreviation | definition
13. Procedure:
Definition = PROC name ( { 0, formal } )
procedure.body
:
Formal = Specifier { 1, name }
| VAL specifier { 1, name }
Procedure.body = Process
Intansce = name ( {0, actual } )
Actual = element
| expression
14. Function :
Value. Process = valof
Valof = VALOF
Process
RESUL expression.list
| specification
valof
Oberend = (value. process )
Expression.list = (value. process )
Definition = { 1, primitive.type }
FUNCTION name ({0, formal})
function.body
:
function.body = value.Process
operand = name (0, expression})
Expression.list = name (0, expression})
Definition = { 1, primitive.type }
FUNCTION name ({0, formal})
IS expression.list :
Caùc ví duï minh hoïa:
/seqmatrix.oc*/
[100][100]INT64 a:
[100][100]INT64 b:
[100][100]INT64 c:
INT64 m:
INT64 dimen:
INT64 sum:
SEQ
dimen :=50
SEQ
SEQ i=0 FOR dimen
SEQ j=0 FOR dimen
a[i][j]:=i*j
SEQ k=0 FOR dimen
SEQ l=0 FOR dimen
b[k][l]:=k+l
SEQ h=0 FOR dimen
SEQ m=0 FOR dimen
c[h][m]:=0
SEQ i= 0 FOR dimen
SEQ k=0 FOR dimen
SEQ
m:=0
SEQ l=0 FOR dimen
m:=m+(a[i][l]*b[l][k])
c[i][k]:=m
sum:=0
SEQ j=0 FOR dimen
SEQ q=0 FOR dimen
SEQ
sum:=sum+c[j][q]
screen ! "Phan tu ket qua c[",j,",",q,"] :",c[j][q],"\n"
screen ! "Tong cac phan tu ma tran :",sum,"\n"
Taøi lieäu tham khaûo
1. OCCAM 2 Refference manual PrenticeHall
A Message Passing Interface Standard
3. Parallel Programs for the Transputer Ronald S.Cok PrenticeHall
4. Parallel Processing ADDISON - WESLEY
M.E.C. Hull
D.Crookes
P.J.Sweenney
5. Compiler, Principles, Technique and Tool
AlfredVAho
REVI SETE
REFREY D ULLMAN
6. COMPILER DISGN IN C
Các file đính kèm theo tài liệu này:
- Mô phỏng hệ thống Transputer trên môi trường MPI.doc