From 58d632edeac87961cbc5628effb10e66b5143d07 Mon Sep 17 00:00:00 2001 From: "Samuel K. Gutierrez" Date: Fri, 12 Jul 2024 15:33:16 -0600 Subject: [PATCH] Test PThread argument forwarding. Signed-off-by: Samuel K. Gutierrez --- tests/test-pthread-split.c | 78 +++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/tests/test-pthread-split.c b/tests/test-pthread-split.c index 8f7661a..cbb6206 100644 --- a/tests/test-pthread-split.c +++ b/tests/test-pthread-split.c @@ -4,13 +4,24 @@ #include "quo-vadis-pthread.h" #include "qvi-test-common.h" +typedef struct { + qv_scope_t *scope; + int answer; +} thargs_t; + void * thread_work( void *arg ) { char const *ers = NULL; const pid_t tid = qvi_test_gettid(); - qv_scope_t *scope = (qv_scope_t *)arg; + thargs_t *thargs = (thargs_t *)arg; + + qv_scope_t *scope = thargs->scope; + if (thargs->answer != 42) { + ers = "user arguments not forwarded!"; + qvi_test_panic("%s", ers); + } char *binds = NULL; int rc = qv_scope_bind_string(scope, QV_BIND_STRING_AS_LIST, &binds); @@ -30,9 +41,7 @@ main(void) { char const *ers = NULL; const pid_t tid = qvi_test_gettid(); - MPI_Comm comm = MPI_COMM_WORLD; int wrank, wsize; - int n_cores = 0; int rc = MPI_Init(NULL, NULL); if (rc != MPI_SUCCESS) { @@ -40,6 +49,7 @@ main(void) qvi_test_panic("%s (rc=%d)", ers, rc); } + MPI_Comm comm = MPI_COMM_WORLD; rc = MPI_Comm_size(comm, &wsize); if (rc != MPI_SUCCESS) { ers = "MPI_Comm_size() failed"; @@ -63,43 +73,53 @@ main(void) qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc)); } - rc = qv_scope_nobjs(mpi_scope, QV_HW_OBJ_CORE, &n_cores); + int ncores = 0; + rc = qv_scope_nobjs(mpi_scope, QV_HW_OBJ_CORE, &ncores); if (rc != QV_SUCCESS) { ers = "qv_scope_nobjs() failed"; qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc)); } //test qv_pthread_scope_split - int npieces = n_cores / 2; - int nthreads = n_cores; + int npieces = ncores / 2; + int nthreads = ncores; fprintf(stdout,"[%d] ====== Testing thread_scope_split (number of threads : %i)\n", tid, nthreads); int colors[nthreads]; - for(int i = 0 ; i < nthreads ; i++) { + for (int i = 0 ; i < nthreads ; i++) { colors[i] = i % npieces; } qv_scope_t **th_scopes = NULL; - rc = qv_pthread_scope_split(mpi_scope, npieces, colors, nthreads, &th_scopes); + rc = qv_pthread_scope_split( + mpi_scope, npieces, colors, nthreads, &th_scopes + ); if (rc != QV_SUCCESS) { ers = "qv_pthread_scope_split() failed"; qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc)); } + thargs_t thargs[nthreads]; + for (int i = 0 ; i < nthreads; i ++) { + thargs[i].scope = th_scopes[i]; + thargs[i].answer = 42; + } + pthread_t thid[nthreads]; pthread_attr_t *attr = NULL; - for (int i = 0 ; i < nthreads; i ++) { - //sleep(1); - if (qv_pthread_create(&thid[i], attr, thread_work, (void *)(th_scopes[i]), th_scopes[i]) != 0) { - perror("pthread_create() error"); - exit(1); + const int ptrc = qv_pthread_create( + &thid[i], attr, thread_work, (void *)&thargs[i], th_scopes[i] + ); + if (ptrc != 0) { + ers = "qv_pthread_create() failed"; + qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc)); } } - void *ret; - for(int i = 0 ; i < nthreads; i ++){ + void *ret = NULL; + for (int i = 0 ; i < nthreads; i ++){ if (pthread_join(thid[i], &ret) != 0) { perror("pthread_create() error"); exit(3); @@ -115,29 +135,41 @@ main(void) } //Test qv_pthread_scope_split_at - nthreads = 2*n_cores; + nthreads = 2 * ncores; fprintf(stdout,"[%d] ====== Testing thread_scope_split_at (number of threads : %i)\n", tid, nthreads); int colors2[nthreads]; - for(int i = 0 ; i < nthreads ; i++) - colors2[i] = i%n_cores; + for (int i = 0 ; i < nthreads ; i++) { + colors2[i] = i % ncores; + } - rc = qv_pthread_scope_split_at(mpi_scope, QV_HW_OBJ_CORE, colors2, nthreads, &th_scopes); + rc = qv_pthread_scope_split_at( + mpi_scope, QV_HW_OBJ_CORE, colors2, nthreads, &th_scopes + ); if (rc != QV_SUCCESS) { ers = "qv_pthread_scope_split_at() failed"; qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc)); } + thargs_t thargs2[nthreads]; + for (int i = 0 ; i < nthreads; i++) { + thargs2[i].scope = th_scopes[i]; + thargs2[i].answer = 42; + } + pthread_t thid2[nthreads]; for(int i = 0 ; i < nthreads; i ++){ - if (qv_pthread_create(&thid2[i], attr, thread_work, (void *)(th_scopes[i]), th_scopes[i]) != 0) { - perror("pthread_create() error"); - exit(1); + const int ptrc = qv_pthread_create( + &thid2[i], attr, thread_work, (void *)&thargs2[i], th_scopes[i] + ); + if (ptrc != 0) { + ers = "qv_pthread_create() failed"; + qvi_test_panic("%s (rc=%s)", ers, qv_strerr(rc)); } } - for(int i = 0 ; i < nthreads; i ++) { + for (int i = 0 ; i < nthreads; i ++) { if (pthread_join(thid2[i], &ret) != 0) { perror("pthread_create() error"); exit(3);