Just for fun.用线程池并行处理ACM多组输入加快运算速度(对于算法提高不推荐)

做一道google code jam的题目来试试看我的线程池构想,看看速度会不会快很多。

其实这题我的算法并不是最优的,所以才会导致10分钟还出不了答案,要是真在code jam上比赛的话妥妥的跪了。
但就算算法不好,我如果充分利用CPU的资源,使用多线程框架,会不会出结果时间快很多呢?试试看吧!

这个纯粹是玩玩的,如果要提高算法能力的话,不建议这么搞

code jam 原题地址戳这里
注:需要翻墙的哦

先来看效果吧:

不使用线程池

cpu负载:

运行结果

不使用线程池,线程池只有1个线程:(20分钟还未输出答案)


使用线程池

cpu负载

运行结果

使用线程池时间:609秒(10个线程处理14个任务)


贴上我的代码:

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// main.cpp
// threadpool
//
// Created by apple on 15/5/1.
// Copyright (c) 2015年 apple. All rights reserved.
//

#include <iostream>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>

#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a < b ? b : a)
typedef long long ll;

/* multithread template */
typedef struct worker{
void *(*process) (void *arg);
void *arg;
struct worker *next;

} CThread_worker;

typedef struct{
pthread_mutex_t queue_lock;
pthread_mutex_t input_lock;
pthread_cond_t queue_ready;
CThread_worker *queue_head;
int shutdown;
pthread_t *threadid;
int max_thread_num;
int cur_queue_size;

} CThread_pool;
static CThread_pool *pool = NULL;

int pool_add_worker (void *(*process) (void *arg), void *arg){
CThread_worker *newworker = (CThread_worker *) malloc (sizeof (CThread_worker));
newworker->process = process;
newworker->arg = arg;
newworker->next = NULL;
pthread_mutex_lock (&(pool->queue_lock));
CThread_worker *member = pool->queue_head;
if (member != NULL){
while (member->next != NULL)
member = member->next;
member->next = newworker;
}else{
pool->queue_head = newworker;
}
assert (pool->queue_head != NULL);
pool->cur_queue_size++;
pthread_mutex_unlock (&(pool->queue_lock));
pthread_cond_signal (&(pool->queue_ready));
return 0;
}

void* thread_routine (void *arg){
fprintf (stderr, "starting thread 0x%lx\n", (unsigned long)pthread_self ());
while (1){
pthread_mutex_lock (&(pool->queue_lock));
while (pool->cur_queue_size == 0 && !pool->shutdown){
fprintf (stderr, "thread 0x%lx is waiting\n", (unsigned long)pthread_self ());
pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock));
fprintf (stderr, "thread 0x%lx wakeup\n", (unsigned long)pthread_self ());
}
if (pool->shutdown){
pthread_mutex_unlock (&(pool->queue_lock));
fprintf (stderr, "thread 0x%lx will exit\n", (unsigned long)pthread_self ());
pthread_exit (NULL);
}
// fprintf (stderr, "thread 0x%lx is starting to work\n", (unsigned long)pthread_self ());
assert (pool->cur_queue_size != 0);
assert (pool->queue_head != NULL);
pool->cur_queue_size--;
CThread_worker *worker = pool->queue_head;
pool->queue_head = worker->next;
pthread_mutex_unlock (&(pool->queue_lock));
(*(worker->process)) (worker->arg);
free (worker);
worker = NULL;
}
}

void pool_init (int max_thread_num){
pool = (CThread_pool *) malloc (sizeof (CThread_pool));
pthread_mutex_init (&(pool->queue_lock), NULL);
pthread_mutex_init (&(pool->input_lock), NULL);
pthread_cond_init (&(pool->queue_ready), NULL);
pool->queue_head = NULL;
pool->max_thread_num = max_thread_num;
pool->cur_queue_size = 0;
pool->shutdown = 0;
pool->threadid = (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
for (int i = 0; i < max_thread_num; ++i){
pthread_create (&(pool->threadid[i]), NULL, thread_routine, NULL);
}
}

int pool_destroy (){
if (pool->shutdown) return -1;/*防止两次调用*/
pool->shutdown = 1;
pthread_cond_broadcast (&(pool->queue_ready));
for (int i = 0; i < pool->max_thread_num; i++)
pthread_join (pool->threadid[i], NULL);
free (pool->threadid);
CThread_worker *head = NULL;
while (pool->queue_head != NULL){
head = pool->queue_head;
pool->queue_head = pool->queue_head->next;
free (head);
}
pthread_mutex_destroy(&(pool->queue_lock));
pthread_cond_destroy(&(pool->queue_ready));
free (pool);
pool = NULL;
return 0;
}

/**********************************************************************************************/

struct Data{
int test_case;
int N;
ll X[3000];
ll Y[3000];
ll ans[3000];
}data[101];

int handleInput(int index){
pthread_mutex_lock (&(pool->input_lock));
scanf("%d", &data[index].N);
int N = data[index].N;
for (int i = 0; i < N; ++i){
scanf("%lld %lld", &data[index].X[i], &data[index].Y[i]);
}
pthread_mutex_unlock (&(pool->input_lock));
return 0;
}

void* mythread (void* arg){
fprintf (stderr, "threadid is 0x%lx, working on task %d\n", (unsigned long)pthread_self (),*(int *) arg);
handleInput(*(int *) arg);
int index = *(int *) arg;
int N = data[index].N;
for (ll i = 0; i < N; ++i){
ll mi = N - 1;
for (int j = 0; j < N; ++j){
if (j == i)continue;
ll x1 = data[index].X[i], x2 = data[index].X[j];
ll y1 = data[index].Y[i], y2 = data[index].Y[j];
ll a = y2 - y1;
ll b = x1 - x2;
ll c = x2 * y1 - x1 * y2;
ll num1 = 0 , num2 =0;
for (ll k = 0; k < N; ++k){
if (k == i || k == j)continue;
ll value = a * data[index].X[k] + b * data[index].Y[k] + c;
if (value > 0){
num1++;
}else if(value < 0){
num2++;
}
}
mi = MIN(mi, num1);
mi = MIN(mi, num2);
}
data[index].ans[i] = mi;
}
return NULL;
}

int main(int argc, const char * argv[]) {
time_t start = time(NULL), end;
pool_init (1);
freopen("/Users/apple/Documents/coding/Xcode/Google Code Jam/threadpool/threadpool/C-large-practice.in.txt", "r", stdin);
freopen("/Users/apple/Documents/coding/Xcode/Google Code Jam/threadpool/threadpool/output2.txt", "w", stdout);
int T; scanf("%d", &T);
for (int i = 1 ; i <= T; ++i){
data[i].test_case = i;
}
for (int i = 1; i <= T; ++i)
{
pool_add_worker (mythread, &data[i].test_case);
}
while(pool->cur_queue_size)sleep(1);
pool_destroy ();
// output
for (int i = 1; i <= T; ++i){
printf("Case #%d:\n", i);
for (int j = 0; j < data[i].N; ++j){
printf("%lld\n", data[i].ans[j]);
}
}
end = time(NULL);
fprintf(stderr, "time : %ld", (end - start));
return 0;
}
Share