source: CIVL/src/include/civl/comm.cvl@ 1f51041

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 1f51041 was d0bf94f, checked in by Ziqing Luo <ziqing@…>, 10 years ago

fix bugs in gbarrier_create and gcomm_destroy

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@3332 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/* This file completes the definitions of some types and functions
2 * for communication, which are declared in comm-common.cvh.
3 */
4
5#ifndef __CIVLC_COMM__
6#define __CIVLC_COMM__
7#include <civlc.cvh>
8#include<comm.cvh>
9#include<seq.cvh>
10/* *********************** Types *********************** */
11
12/* A datatype representing a queue of messages. All message
13 * data is encapsulated inside this value; no external allocation
14 * is used. Completes the declaration of this structure type in
15 * civlc-common.h */
16struct $queue {
17 int length;
18 $message messages[];
19};
20
21
22/* A global communicator datatype which must be operated by local communicators.
23 * This communicator type has the same meaning as the communicator type
24 * in MPI. Completes the declaration of this type in civlc-common.h */
25struct _gcomm_ {
26 int nprocs; // number of processes
27 $proc procs[];
28 _Bool isInit[]; // if the local comm has been initiated
29 $queue buf[][]; // message buffers
30};
31
32/* A datatype representing a local communicator which is used for
33 * operating global communicators. The local communicator type has
34 * a handle of a global communicator. This type represents for
35 * a set of processes which have ranks in common.
36 * Completes the declaration of this type in civlc-common.h.
37 */
38struct _comm_ {
39 int place;
40 $gcomm gcomm;
41};
42
43/* *********************** Functions *********************** */
44
45/* creates a new message, copying data from the specified buffer */
46$message $message_pack(int source, int dest, int tag,
47 void *data, int size) {
48 $message result;
49
50 result.source = source;
51 result.dest = dest;
52 result.tag = tag;
53 result.data = $bundle_pack(data, size);
54 result.size = size;
55 return result;
56}
57
58/* returns the message source */
59int $message_source($message message) {
60 return message.source;
61}
62
63/* returns the message tag */
64int $message_tag($message message) {
65 return message.tag;
66}
67
68/* returns the message destination */
69int $message_dest($message message) {
70 return message.dest;
71}
72
73/* returns the message size */
74int $message_size($message message) {
75 return message.size;
76}
77
78/* transfers message data to buf, throwing exception if message
79 * size exceeds specified size */
80void $message_unpack($message message, void *buf, int size) {
81 $bundle_unpack(message.data, buf);
82 $assert(message.size <= size, "Message of size %d exceeds the specified size %d.", message.size, size);
83}
84
85/*@ depends_on \nothing;
86 @ assigns \nothing;
87 @ reads \nothing;
88 @*/
89$atomic_f $gcomm $gcomm_create($scope scope, int size){
90 $gcomm gcomm=($gcomm)$malloc(scope, sizeof(struct _gcomm_));
91 $queue empty;
92
93 empty.length=0;
94 $seq_init(&empty.messages, 0, NULL);
95 gcomm->nprocs=size;
96 gcomm->procs=(($proc[size])$lambda(int i)$proc_null);
97 gcomm->isInit=((_Bool[size])$lambda(int i)$false);
98 gcomm->buf=(($queue[size][size])$lambda(int i,j) empty);
99 return gcomm;
100}
101
102/*@ depends_on \access(junkMsgs), \access(gcomm);
103 @ assigns junkMsgs, gcomm;
104 @ reads \nothing;
105 @*/
106$atomic_f int $gcomm_destroy($gcomm gcomm, void * junkMsgs){
107 int nprocs = gcomm->nprocs;
108 int numJunks = 0;
109
110 if(junkMsgs != NULL) {
111 for (int i = 0; i < nprocs; i++)
112 for (int j = 0; j < nprocs; j++) {
113 $queue queue = gcomm->buf[i][j];
114
115 if (queue.length > 0)
116 $seq_append(junkMsgs, queue.messages, queue.length);
117 }
118 numJunks = $seq_length(junkMsgs);
119 }
120 $free(gcomm);
121 return numJunks;
122}
123
124/*@ depends_on \nothing;
125 @ reads gcomm;
126 @ assigns gcomm;
127 @*/
128$atomic_f $comm $comm_create($scope scope, $gcomm gcomm, int place){
129 $assert(!gcomm->isInit[place], "the place %d is already occupied in the global communicator!", place);
130
131 $comm comm=($comm)$malloc(scope, sizeof(struct _comm_));
132
133 gcomm->procs[place]=$self;
134 gcomm->isInit[place]=$true;
135 comm->gcomm=gcomm;
136 comm->place=place;
137 return comm;
138}
139
140/*@ depends_on \access(comm);
141 @ assigns comm;
142 @ reads \nothing;
143 @*/
144$atomic_f void $comm_destroy($comm comm){
145 $free(comm);
146}
147
148/* Returns the place of the local communicator. This is the same as the
149 * place argument used to create the local communicator. */
150int $comm_place($comm comm){
151 return comm->place;
152}
153
154#endif
Note: See TracBrowser for help on using the repository browser.