1 //
2 // messint.cc
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27
28 #include <util/misc/formio.h>
29 #include <util/group/message.h>
30
31 using namespace std;
32 using namespace sc;
33
34 static ClassDesc intMessageGrp_cd(
35 typeid(intMessageGrp),"intMessageGrp",1,"public MessageGrp",
36 0, 0, 0);
37
intMessageGrp()38 intMessageGrp::intMessageGrp():
39 ctl_nbit(2),
40 ctl_mask(0x3)
41 {
42 }
43
intMessageGrp(const Ref<KeyVal> & keyval)44 intMessageGrp::intMessageGrp(const Ref<KeyVal>& keyval):
45 MessageGrp(keyval),
46 ctl_nbit(2),
47 ctl_mask(0x3)
48 {
49 }
50
~intMessageGrp()51 intMessageGrp::~intMessageGrp()
52 {
53 delete[] source_seq;
54 delete[] target_seq;
55 }
56
57 void
initialize(int me,int n,int nbits)58 intMessageGrp::initialize(int me, int n, int nbits)
59 {
60 int i;
61
62 // Initialize the arrays storing the next sequence number.
63 source_seq = new int[n];
64 target_seq = new int[n];
65 for (i=0; i<n; i++) {
66 source_seq[i] = 0;
67 target_seq[i] = 0;
68 }
69
70 // Find how many bits are needed to store the source information.
71 int tmp = 1;
72 for (i=1; tmp<n; i++) {
73 tmp = tmp<<1;
74 }
75 src_nbit = i;
76
77 // The remaining bits are for sequence information.
78 seq_nbit = nbits - ctl_nbit - src_nbit;
79 if (seq_nbit < 8) {
80 ExEnv::errn() << "intMessageGrp: not enough bits in underlying msgtype" << endl;
81 abort();
82 }
83
84 // The bits available to user defined messages overlap
85 // with the seq and the src bits.
86 typ_nbit = seq_nbit + src_nbit;
87
88 // Compute the shifts needed to construct a message type.
89 seq_shift = 0;
90 typ_shift = 0;
91 src_shift = seq_shift + seq_nbit;
92 ctl_shift = src_shift + src_nbit;
93
94 // Compute the masks for each field.
95 seq_mask = (1<<seq_nbit) - 1;
96 typ_mask = (1<<typ_nbit) - 1;
97 src_mask = (1<<src_nbit) - 1;
98
99 // Complete initialization of the base class.
100 MessageGrp::initialize(me, n);
101 }
102
103 int
msgtype_typ(int msgtype)104 intMessageGrp::msgtype_typ(int msgtype)
105 {
106 return msgtype>>typ_shift & typ_mask;
107 }
108
109 int
typ_msgtype(int usrtype)110 intMessageGrp::typ_msgtype(int usrtype)
111 {
112 return usrtype<<typ_shift | 1<<ctl_shift;
113 }
114
115 int
seq_msgtype(int source,int seq)116 intMessageGrp::seq_msgtype(int source, int seq)
117 {
118 return source<<src_shift | seq<<seq_shift | 2<<ctl_shift;
119 }
120
121 void
raw_send(int target,const void * data,int nbyte)122 intMessageGrp::raw_send(int target, const void* data, int nbyte)
123 {
124 int& seq = target_seq[target];
125 int msgtype = seq_msgtype(me(),seq);
126 #ifdef DEBUG
127 ExEnv::outn() << scprintf("node %d sending to %d(%d) msgtype = %d\n",
128 me(),target,seq,msgtype);
129 #endif
130 basic_send(target, msgtype, data, nbyte);
131 if (seq >= seq_mask) seq = 0;
132 else seq++;
133 }
134
135 void
raw_recv(int sender,void * data,int nbyte)136 intMessageGrp::raw_recv(int sender, void* data, int nbyte)
137 {
138 int& seq = source_seq[sender];
139 int msgtype = seq_msgtype(sender,seq);
140 #ifdef DEBUG
141 ExEnv::outn() << scprintf("node %d receiving from %d(%d) msgtype = %d\n",
142 me(),sender,seq,msgtype);
143 #endif
144 basic_recv(msgtype, data, nbyte);
145 #ifdef DEBUG
146 ExEnv::outn() << scprintf("node %d received %d\n",me(),msgtype);
147 #endif
148 if (seq >= seq_mask) seq = 0;
149 else seq++;
150 }
151
152 void
raw_sendt(int target,int msgtype,const void * data,int nbyte)153 intMessageGrp::raw_sendt(int target, int msgtype, const void* data, int nbyte)
154 {
155 basic_send(target, typ_msgtype(msgtype), data, nbyte);
156 }
157
158 void
raw_recvt(int type,void * data,int nbyte)159 intMessageGrp::raw_recvt(int type, void* data, int nbyte)
160 {
161 basic_recv(typ_msgtype(type), data, nbyte);
162 }
163
164 int
probet(int type)165 intMessageGrp::probet(int type)
166 {
167 return basic_probe(typ_msgtype(type));
168 }
169
170 int
leftover_ctl_bits()171 intMessageGrp::leftover_ctl_bits()
172 {
173 return 3 << ctl_shift;
174 }
175
176 /////////////////////////////////////////////////////////////////////////////
177
178 // Local Variables:
179 // mode: c++
180 // c-file-style: "CLJ"
181 // End:
182