forked from jeffhammond/foMPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fompi_notif_uq.c
169 lines (143 loc) · 4.47 KB
/
fompi_notif_uq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* fompi_notif_uq.c
*
* Created on: Jul 14, 2014
* Author: Roberto Belli
*/
#include <stdio.h>
#include "fompi.h"
#ifdef UGNI
#include "fompi_notif_uq.h"
static inline int _uq_remove_tail(fompi_notif_uq_t *uq, fompi_notif_node_t** node);
static inline void _uq_insert_head(fompi_notif_uq_t *uq, fompi_notif_node_t* node);
static inline void _uq_node_detach(fompi_notif_uq_t *uq, fompi_notif_node_t* node);
static inline fompi_notif_node_t* _uq_node_create(uint16_t source, uint16_t tag);
static inline void _uq_node_free(fompi_notif_node_t* node);
static inline int _fompi_notif_uq_search_remove_internal(fompi_notif_uq_t *uq, uint16_t source_in , uint16_t *source_out ,int wildcard_source, uint16_t tag_in, uint16_t *tag_out, int wildcard_tag);
int _fompi_notif_uq_isEmpty(fompi_notif_uq_t *uq){
if(uq->size==0)
return _foMPI_TRUE;
return _foMPI_FALSE;
}
fompi_notif_uq_t* _fompi_notif_uq_init() {
fompi_notif_uq_t *uq = _foMPI_ALLOC(sizeof(fompi_notif_uq_t));
uq->head = NULL;
uq->tail = NULL;
uq->size = 0;
return uq;
}
void _fompi_notif_uq_finalize(fompi_notif_uq_t **uq) {
fompi_notif_node_t *node;
int res = _uq_remove_tail(*uq, &node);
while (res != _foMPI_NO_MATCH) {
_uq_node_free(node);
res = _uq_remove_tail(*uq, &node);
}
*uq = NULL;
return;
}
int _fompi_notif_uq_push(fompi_notif_uq_t *uq, uint16_t source, uint16_t tag) {
fompi_notif_node_t *node = _uq_node_create(source, tag);
_uq_insert_head(uq, node);
return MPI_SUCCESS;
}
/**/
int _fompi_notif_uq_pop(fompi_notif_uq_t *uq, uint16_t *source, uint16_t *tag) {
fompi_notif_node_t *node;
if (_uq_remove_tail(uq, &node) != _foMPI_NO_MATCH) {
*source = node->source;
*tag = node->tag;
_uq_node_free(node);
return MPI_SUCCESS;
}
return _foMPI_NO_MATCH;
}
int _fompi_notif_uq_search_remove(fompi_notif_uq_t *uq, uint16_t source,uint16_t tag){
uint16_t src = 0, tg= 0; /*dummy values*/
return _fompi_notif_uq_search_remove_internal(uq, source, &src,0 , tag, &tg, 0);
}
int _fompi_notif_uq_search_remove_tag(fompi_notif_uq_t *uq, uint16_t tag, uint16_t *source) {
uint16_t src = 0, tg= 0; /*dummy values*/
return _fompi_notif_uq_search_remove_internal(uq, src , source, 1 , tag, &tg, 0);
}
int _fompi_notif_uq_search_remove_src(fompi_notif_uq_t *uq, uint16_t source, uint16_t *tag) {
uint16_t src = 0, tg= 0; /*dummy values*/
return _fompi_notif_uq_search_remove_internal(uq, source , &src, 0 , tg, tag, 1);
}
static inline int _fompi_notif_uq_search_remove_internal(fompi_notif_uq_t *uq, uint16_t source_in , uint16_t *source_out ,int wildcard_source, uint16_t tag_in, uint16_t *tag_out, int wildcard_tag) {
if (uq->size == 0) {
return _foMPI_NO_MATCH;
}
fompi_notif_node_t *node;
node = uq->tail;
while (node != NULL) {
if ((wildcard_source || node->source == source_in) && (wildcard_tag || node->tag == tag_in)) {
/*notification found */
_uq_node_detach(uq, node);
*source_out = node->source;
*tag_out = node->tag;
_uq_node_free(node);
return MPI_SUCCESS;
}
node=node->prev;
}
return _foMPI_NO_MATCH;
}
static inline int _uq_remove_tail(fompi_notif_uq_t *uq, fompi_notif_node_t** node) {
if (uq->size > 0) {
(*node) = uq->tail;
if((*node)->prev !=NULL){
(*node)->prev->next = NULL;
} else {
uq->head = NULL;
}
uq->tail = (*node)->prev;
uq->size--;
return MPI_SUCCESS;
}
return _foMPI_NO_MATCH;
}
static inline void _uq_insert_head(fompi_notif_uq_t *uq, fompi_notif_node_t* node) {
node->next = uq->head;
if(uq->head != NULL){
uq->head->prev = node;
} else {
uq->tail = node;
}
uq->head = node;
node->prev = NULL;
uq->size++;
}
static inline void _uq_node_detach(fompi_notif_uq_t *uq, fompi_notif_node_t* node) {
if (node != uq->tail && node != uq->head) {
/*the node is in th middle of the list*/
node->prev->next = node->next;
node->next->prev = node->prev;
}
if (node == uq->tail && node == uq->head) {
/*the node is the only element on the list*/
uq->tail = NULL;
uq->head = NULL;
}
if (node == uq->tail) {
/*the node is the last element on the list*/
uq->tail = node->prev;
node->prev->next = NULL;
}
if (node == uq->head) {
/*the node is the first element on the list*/
uq->head = node->next;
node->next->prev = NULL;
}
uq->size--;
}
static inline fompi_notif_node_t* _uq_node_create(uint16_t source, uint16_t tag) {
fompi_notif_node_t* node = _foMPI_ALLOC(sizeof(fompi_notif_node_t));
node->source = source;
node->tag = tag;
return node;
}
static inline void _uq_node_free(fompi_notif_node_t* node) {
_foMPI_FREE(node);
}
#endif