-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE_thread.cpp
More file actions
102 lines (73 loc) · 2.84 KB
/
Copy pathE_thread.cpp
File metadata and controls
102 lines (73 loc) · 2.84 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <cstdlib>
#include <map>
#include <algorithm> // std::sort
#include <math.h>
#include <iomanip>
#include "F2M.h"
//##################################################
//##################################################
//## MULTITHREAD
//##################################################
typedef struct {
int thread_id;
int i1;
int i2;
std::vector<edge*> * E;
double * F;
int type ;
} thread_argument;
void * runthread_EF(void *arg) {
thread_argument* tArg =(thread_argument*)arg;
int thread_id = tArg->thread_id;
int i1 = tArg->i1;
int i2 = tArg->i2;
std::vector<edge*> * E = tArg->E;
double * F = tArg->F;
int type = tArg->type;
if(type == 1){for(int i=i1; i < i2; ++i ){(*E)[i]->d = (*E)[i]->D + F[(*E)[i]->v1->id]/2 + F[(*E)[i]->v2->id]/2;}}
pthread_exit(NULL);
}
void graph::EF_thread(int k_THREADS , int type)
{
pthread_t threads[k_THREADS];
int rc;
int i;
pthread_attr_t attr;
void *status;
// Initialize and set thread joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
int thread_size = 0;
if(type == 1){ thread_size = (E.size() / k_THREADS) + 1;}
//if(type == 2){ thread_size = (E.size() / k_THREADS) + 1;}
//if(type == 3){ thread_size = (E.size() / k_THREADS) + 1;}
std::cout << "thread_size, " << thread_size << " " << E.size() << "\n";
for(int i=0; i < k_THREADS; ++i ){
//std::cout << "main() : creating thread, " << i << "\n";
thread_argument* arg = new thread_argument();
arg->thread_id = i;
arg->i1 = (i )*thread_size;
arg->i2 = (i+1)*thread_size;
arg->F = F;
arg->E = & E;
arg->type = type;
if(type == 1){if(arg->i2 > E.size() ){arg->i2 = E.size();}}
//if(type == 2){if(arg->i2 > E.size() ){arg->i2 = E.size();}}
//if(type == 3){if(arg->i2 > E.size() ){arg->i2 = E.size();}}
pthread_create(&threads[i], NULL, runthread_EF, arg);
}
// free attribute and wait for the other threads
for(int i=0; i < k_THREADS; ++i ){
rc = pthread_join(threads[i], &status);
if(rc){std::cout << "Error:unable to join," << rc << "\n";exit(-1);}
}
}
//##################################################
//##################################################
//##################################################