Skip to content

Commit 6d70320

Browse files
- New interface for pthreads. (#79)
- The former layout interface is kept for now but will be removed in a forthcoming clean-up Signed-off-by: Guillaume Mercier <[email protected]>
1 parent 8b2a348 commit 6d70320

File tree

5 files changed

+137
-116
lines changed

5 files changed

+137
-116
lines changed

include/quo-vadis-thread.h

+46-19
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,51 @@ typedef enum qv_policy_s {
5353
QV_POLICY_CHOOSE
5454
} qv_policy_t;
5555

56+
57+
/**
58+
* Creates a thread context.
59+
*/
60+
int
61+
qv_thread_context_create(
62+
qv_context_t **ctx
63+
);
64+
65+
66+
/**
67+
* Frees resources associated with a context created by
68+
* qv_thread_context_create().
69+
*/
70+
int
71+
qv_thread_context_free(
72+
qv_context_t *ctx
73+
);
74+
75+
76+
#ifndef USE_LAYOUTS
77+
typedef struct {
78+
qv_context_t *ctx;
79+
qv_scope_t *scope;
80+
void *(*thread_routine)(void *);
81+
void *arg;
82+
} qv_thread_args_t;
83+
84+
85+
void *
86+
qv_thread_routine(
87+
void * arg
88+
);
89+
90+
91+
int
92+
qv_pthread_create(
93+
pthread_t *thread,
94+
pthread_attr_t *attr,
95+
void *(*thread_routine)(void *arg),
96+
void *arg,
97+
qv_context_t *ctx,
98+
qv_scope_t *scope
99+
);
100+
#else
56101
/**
57102
* Layout for fine-grain binding
58103
* with default behaviour
@@ -93,25 +138,6 @@ typedef struct {
93138
int num_th;
94139
} qv_thread_args_t;
95140

96-
97-
/**
98-
* Creates a thread context.
99-
*/
100-
int
101-
qv_thread_context_create(
102-
qv_context_t **ctx
103-
);
104-
105-
106-
/**
107-
* Frees resources associated with a context created by
108-
* qv_thread_context_create().
109-
*/
110-
int
111-
qv_thread_context_free(
112-
qv_context_t *ctx
113-
);
114-
115141
int
116142
qv_thread_layout_create(
117143
qv_context_t *ctx,
@@ -160,6 +186,7 @@ qv_thread_layout_set_stride(
160186
qv_layout_t *layout,
161187
int stride
162188
);
189+
#endif // USE_LAYOUTS
163190

164191

165192
#ifdef __cplusplus

src/quo-vadis-thread.cc

+62-21
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,76 @@ qv_thread_context_create(
8989
return rc;
9090
}
9191

92+
#ifndef USE_LAYOUTS
93+
//New interface
94+
void *
95+
qv_thread_routine(
96+
void * arg
97+
) {
98+
qv_thread_args_t *arg_ptr = (qv_thread_args_t *) arg;
99+
// fprintf(stdout,"qv_thread_routine: ctx=%p scope=%p\n", qvp->ctx, qvp->scope);
100+
101+
int rc = qv_bind_push(arg_ptr->ctx, arg_ptr->scope);
102+
if (rc != QV_SUCCESS) {
103+
// char const *ers = "qv_bind_push() failed";
104+
// qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc));
105+
pthread_exit(NULL);
106+
}
107+
108+
void *ret = arg_ptr->thread_routine(arg_ptr->arg);
109+
110+
// Free memory allocated in qv_pthread_create
111+
//free(arg_ptr);
112+
delete arg_ptr;
113+
pthread_exit(ret);
114+
}
115+
116+
int
117+
qv_pthread_create(
118+
pthread_t *thread,
119+
pthread_attr_t *attr,
120+
void *(*thread_routine)(void *arg),
121+
void *arg,
122+
qv_context_t *ctx,
123+
qv_scope_t *scope
124+
) {
125+
// Memory will be freed in qv_thread_routine to avoid memory leaks
126+
qv_thread_args_t *arg_ptr = qvi_new qv_thread_args_t();
127+
//(qv_thread_args_t *)malloc(sizeof(qv_thread_args_t));
128+
arg_ptr->ctx = ctx;
129+
arg_ptr->scope = scope;
130+
arg_ptr->thread_routine = thread_routine;
131+
arg_ptr->arg = arg;
132+
133+
// fprintf(stdout,"qv_pthread_create: ctx=%p scope=%p\n", ctx, scope);
134+
return pthread_create(thread, attr, qv_thread_routine, arg_ptr);
135+
}
136+
#else // USE_LAYOUTS
137+
// Layout interface
92138
int
93139
qv_thread_layout_create( // use hwpool if necessary
94140
qv_context_t *ctx,
95141
qv_layout_params_t params,
96142
qv_layout_t **layout
97-
)
98-
{
99-
int rc = QV_SUCCESS;
100-
qv_layout_t *ilay = qvi_new qv_layout_t();
143+
) {
144+
int rc = QV_SUCCESS;
145+
qv_layout_t *ilay = qvi_new qv_layout_t();
101146

102-
ilay->hwl = qvi_rmi_client_hwloc_get(ctx->rmi);
103-
ilay->hw_topo = qvi_hwloc_get_topo_obj(ilay->hwl);
104-
ilay->params = params;
105-
ilay->ctx = ctx;
106-
memset(&ilay->cached_info,0,sizeof(qv_layout_cached_info_t));
107-
ilay->is_cached = 0;
147+
ilay->hwl = qvi_rmi_client_hwloc_get(ctx->rmi);
148+
ilay->hw_topo = qvi_hwloc_get_topo_obj(ilay->hwl);
149+
ilay->params = params;
150+
ilay->ctx = ctx;
151+
memset(&ilay->cached_info,0,sizeof(qv_layout_cached_info_t));
152+
ilay->is_cached = 0;
108153

109-
*layout = ilay;
110-
return rc;
154+
*layout = ilay;
155+
return rc;
111156
}
112157

113158
int
114159
qv_thread_layout_free(
115160
qv_layout_t *layout
116-
)
117-
{
161+
){
118162
int rc = QV_SUCCESS;
119163

120164
if (!layout) {
@@ -134,8 +178,7 @@ int
134178
qv_thread_layout_set_policy(
135179
qv_layout_t *layout,
136180
qv_policy_t policy
137-
)
138-
{
181+
) {
139182
int rc = QV_SUCCESS;
140183

141184
if (!layout) {
@@ -153,8 +196,7 @@ int
153196
qv_thread_layout_set_obj_type(
154197
qv_layout_t *layout,
155198
qv_hw_obj_type_t obj_type
156-
)
157-
{
199+
) {
158200
int rc = QV_SUCCESS;
159201

160202
if (!layout) {
@@ -172,8 +214,7 @@ int
172214
qv_thread_layout_set_stride(
173215
qv_layout_t *layout,
174216
int stride
175-
)
176-
{
217+
) {
177218
int rc = QV_SUCCESS;
178219

179220
if (!layout) {
@@ -381,7 +422,7 @@ qv_thread_args_set(
381422
return rc;
382423
}
383424

384-
425+
#endif // USE_LAYOUTS
385426

386427
/*
387428
* vim: ft=cpp ts=4 sts=4 sw=4 expandtab

src/qvi-thread.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ static int qvi_get_subgroup_info(
586586
idx2++;
587587
*new_id = (idx2 - idx);
588588

589-
#pragma omp barrier // to prevent the quickest hread to remove data before all others have used it
589+
#pragma omp barrier // to prevent the quickest thread to remove data before all others have used it
590590
#pragma omp single
591591
delete [] lptr;
592592
return QV_SUCCESS;

tests/CMakeLists.txt

+27-26
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ if(MPI_FOUND)
215215

216216
target_link_libraries(
217217
test-progress-thread
218+
quo-vadis
218219
quo-vadis-mpi
219220
)
220221

@@ -263,18 +264,18 @@ if(MPI_FOUND)
263264

264265
############################# HYBRID TESTS (PThreads) ##########################
265266
################################################################################
266-
add_executable(
267-
test-mpi-pthreads-layout
268-
qvi-test-common.h
269-
test-mpi-pthreads-layout.c
270-
)
267+
#add_executable(
268+
#test-mpi-pthreads-layout
269+
#qvi-test-common.h
270+
#test-mpi-pthreads-layout.c
271+
#)
271272

272273

273-
target_link_libraries(
274-
test-mpi-pthreads-layout
275-
quo-vadis
276-
quo-vadis-mpi
277-
)
274+
#target_link_libraries(
275+
#test-mpi-pthreads-layout
276+
#quo-vadis
277+
#quo-vadis-mpi
278+
#)
278279

279280
############################# HYBRID TESTS (OpenMP) ############################
280281
################################################################################
@@ -292,19 +293,19 @@ if(MPI_FOUND)
292293
OpenMP::OpenMP_C
293294
)
294295

295-
add_executable(
296-
test-mpi-threads-layout
297-
qvi-test-common.h
298-
test-mpi-threads-layout.c
299-
)
296+
#add_executable(
297+
# test-mpi-threads-layout
298+
# qvi-test-common.h
299+
# test-mpi-threads-layout.c
300+
#)
300301

301302

302-
target_link_libraries(
303-
test-mpi-threads-layout
304-
quo-vadis
305-
quo-vadis-mpi
306-
OpenMP::OpenMP_C
307-
)
303+
#target_link_libraries(
304+
# test-mpi-threads-layout
305+
# quo-vadis
306+
# quo-vadis-mpi
307+
# OpenMP::OpenMP_C
308+
#)
308309

309310
endif()
310311
endif()
@@ -376,7 +377,7 @@ if(MPI_FOUND)
376377
test-mpi-api
377378
test-mpi-getdev
378379
test-mpi-scopes
379-
test-mpi-pthreads-layout
380+
#test-mpi-pthreads-layout
380381
PROPERTIES LINKER_LANGUAGE C
381382
)
382383

@@ -385,10 +386,10 @@ if(MPI_FOUND)
385386
test-mpi-threads
386387
PROPERTIES LINKER_LANGUAGE C
387388
)
388-
set_target_properties(
389-
test-mpi-threads-layout
390-
PROPERTIES LINKER_LANGUAGE C
391-
)
389+
#set_target_properties(
390+
# test-mpi-threads-layout
391+
# PROPERTIES LINKER_LANGUAGE C
392+
#)
392393
endif()
393394
endif()
394395

tests/test-progress-thread.c

+1-49
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,9 @@
22
#include <stdio.h>
33
#include <pthread.h>
44
#include "quo-vadis-mpi.h"
5+
#include "quo-vadis-thread.h"
56
#include "qvi-test-common.h"
67

7-
8-
/*
9-
* It would be nice to have these defined as part of QV.
10-
* It would simplify usage and avoid errors.
11-
*/
12-
13-
typedef struct {
14-
qv_context_t *ctx;
15-
qv_scope_t *scope;
16-
void *(*thread_routine)(void *);
17-
void *arg;
18-
} qv_thread_args_t;
19-
20-
void *qv_thread_routine(void * arg)
21-
{
22-
qv_thread_args_t *qvp = (qv_thread_args_t *) arg;
23-
// printf("qv_thread_routine: ctx=%p scope=%p\n", qvp->ctx, qvp->scope);
24-
25-
int rc = qv_bind_push(qvp->ctx, qvp->scope);
26-
if (rc != QV_SUCCESS) {
27-
char const *ers = "qv_bind_push() failed";
28-
qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc));
29-
}
30-
31-
qvp->thread_routine(qvp->arg);
32-
33-
/* Memory allocated in qv_pthread_create */
34-
free(qvp);
35-
36-
pthread_exit(NULL);
37-
}
38-
39-
int qv_pthread_create(pthread_t *thread, pthread_attr_t *attr,
40-
void *(*thread_routine)(void *arg), void *arg,
41-
qv_context_t *ctx, qv_scope_t *scope)
42-
{
43-
/* Memory will be freed in qv_thread_routine to avoid
44-
a memory leak */
45-
qv_thread_args_t *qv_thargs = malloc(sizeof(qv_thread_args_t));
46-
qv_thargs->ctx = ctx;
47-
qv_thargs->scope = scope;
48-
qv_thargs->thread_routine = thread_routine;
49-
qv_thargs->arg = arg;
50-
51-
// printf("qv_pthread_create: ctx=%p scope=%p\n", ctx, scope);
52-
return pthread_create(thread, attr, qv_thread_routine, qv_thargs);
53-
}
54-
55-
568
/*
579
* QV Todo:
5810
*

0 commit comments

Comments
 (0)