Ice Sheet System Model  4.18
Code documentation
Data Structures | Functions
issm_threads.h File Reference

prototypes for issm_threads.h More...

Go to the source code of this file.

Data Structures

struct  pthread_handle
 

Functions

void LaunchThread (void *function(void *), void *gate, int num_threads)
 
void PartitionRange (int *pi0, int *pi1, int num_el, int num_threads, int my_thread)
 

Detailed Description

prototypes for issm_threads.h

Definition in file issm_threads.h.

Function Documentation

◆ LaunchThread()

void LaunchThread ( void *  functionvoid *,
void *  gate,
int  num_threads 
)

Definition at line 25 of file LaunchThread.cpp.

25  {
26 
27  #ifdef _MULTITHREADING_
28  int i;
29  int *status = NULL;
30  pthread_t *threads = NULL;
31  pthread_handle *handles = NULL;
32 
33  /*check number of threads*/
34  if(num_threads<1) _error_("number of threads must be at least 1");
35  if(num_threads>2000) _error_("number of threads seems to be too high ("<<num_threads<<">2000)");
36 
37  /*dynamically allocate: */
38  threads=xNew<pthread_t>(num_threads);
39  handles=xNew<pthread_handle>(num_threads);
40 
41  for(i=0;i<num_threads;i++){
42  handles[i].gate=gate;
43  handles[i].id=i;
44  handles[i].num=num_threads;
45  }
46  for(i=0;i<num_threads;i++){
47 
48  if(pthread_create(threads+i,NULL,function,(void*)(handles+i))){
49  _error_("pthread_create error");
50  }
51  }
52  for(i=0;i<num_threads;i++){
53  if(pthread_join(threads[i],(void**)&status)){
54  _error_("pthread_join error");
55  }
56  }
57 
58  /*Free ressources:*/
59  xDelete<pthread_t>(threads);
60  xDelete<pthread_handle>(handles);
61 
62  #else
63  pthread_handle handle;
64  handle.gate=gate;
65  handle.id=0;
66  handle.num=1;
67 
68  function((void*)&handle);
69  #endif
70 }

◆ PartitionRange()

void PartitionRange ( int *  pi0,
int *  pi1,
int  num_el,
int  num_threads,
int  my_thread 
)

Definition at line 13 of file PartitionRange.cpp.

13  {
14 
15  /*output: */
16  int i0=-1;
17  int i1=-1;
18 
19  int step;
20  int i;
21 
22  /*distribute elements across threads :*/
23  step=(int)floor((double)num_el/(double)num_threads);
24  for(i=0;i<(my_thread+1);i++){
25  i0=i*step;
26  if(i==(num_threads-1))i1=num_el;
27  else i1=i0+step;
28  }
29 
30  /*Assign output pointers:*/
31  *pi0=i0;
32  *pi1=i1;
33 }
pthread_handle::id
int id
Definition: issm_threads.h:12
pthread_handle::num
int num
Definition: issm_threads.h:13
pthread_handle
Definition: issm_threads.h:10
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
pthread_handle::gate
void * gate
Definition: issm_threads.h:11