首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

五种基于 MapReduce 的并行计算框架介绍及性能测试(6)Phoenix WordCount 实验

五种基于 MapReduce 的并行计算框架介绍及性能测试(6)Phoenix WordCount 实验

Phoenix                WordCount 实验Phoenix 是基于 CPU 的 MapReduce 框架,所以它也是采用将数据分割后读入内存,然后开始 MapReduce 处理阶段这样的传统方式。Phoenix                并不由用户决定切分每个 Map 分配到的数据块的大小,它是根据集群系统的实际 Cache 大小来切分的,这样可以避免出现分配到 Map                的数据块过大或者过小的情况出现。过大的数据快会导致 Map 执行较慢,过小的数据快会导致 Map 资源浪费,因为每次启动 Map 线程都需要消耗一定的系统资源。Map                阶段切分好的文本被多个 Map 并行执行,Phoenix 支持 100 个左右的 Map 并行执行,一个工作节点下可以有若干个 Map                并行执行。只有当一个工作节点上所有的 Map 任务都结束后才开始 Reduce 阶段。Reduce                阶段继续沿用了动态任务调度机制,同时允许用户自定义数据分区规则。
清单 6 .                    Phoenix 的 wordCount 程序段
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <ctype.h>
#include <inttypes.h>
#include "map_reduce.h"
#include "stddefines.h"
#include "sort.h"
#define DEFAULT_DISP_NUM 10
typedef struct {
int fpos;
off_t flen;
char *fdata;
int unit_size;
} wc_data_t;
enum {
IN_WORD,
NOT_IN_WORD
};
struct timeval begin, end;
#ifdef TIMING
unsigned int library_time = 0;
#endif
/** mystrcmp()
* Comparison function to compare 2 words
*/
int mystrcmp(const void *s1, const void *s2)
{
return strcmp((const char *)s1, (const char *) s2);
}
/** mykeyvalcmp()
* Comparison function to compare 2 ints
*/
int mykeyvalcmp(const void *v1, const void *v2)
{
keyval_t* kv1 = (keyval_t*)v1;
keyval_t* kv2 = (keyval_t*)v2;
intptr_t *i1 = kv1->val;
intptr_t *i2 = kv2->val;
if (i1 < i2) return 1;
else if (i1 > i2) return -1;
else {
return strcmp((char *)kv1->key, (char *)kv2->key);
//return 0;
}
}
/** wordcount_分割器 ()
* 内存里面进行 Map 计算
*/
int wordcount_splitter(void *data_in, int req_units, map_args_t *out)
{
wc_data_t * data = (wc_data_t *)data_in;
assert(data_in);
assert(out);
assert(data->flen >= 0);
assert(data->fdata);
assert(req_units);
assert(data->fpos >= 0);
// End of file reached, return FALSE for no more data
if (data->fpos >= data->flen) return 0;
// Set the start of the next data
out->data = (void *)&data->fdata[data->fpos];
// Determine the nominal length
out->length = req_units * data->unit_size;
if (data->fpos + out->length > data->flen)
out->length = data->flen - data->fpos;
// Set the length to end at a space
for (data->fpos += (long)out->length;
data->fpos < data->flen &&
data->fdata[data->fpos] != ' ' && data->fdata[data->fpos] != '\t' &&
data->fdata[data->fpos] != '\r' && data->fdata[data->fpos] != '\n';
data->fpos++, out->length++);
return 1;
}
/** wordcount_locator()
* Return the memory address where this map task would heavily access.
*/
void *wordcount_locator (map_args_t *task)
{
assert (task);
return task->data;
}
/** wordcount_map()
* 对文本进行计数
*/
void wordcount_map(map_args_t *args)
{
char *curr_start, curr_ltr;
int state = NOT_IN_WORD;
int i;
assert(args);
char *data = (char *)args->data;
assert(data);
curr_start = data;
for (i = 0; i < args->length; i++)
{
curr_ltr = toupper(data);
switch (state)
{
case IN_WORD:
data = curr_ltr;
if ((curr_ltr < 'A' || curr_ltr > 'Z') && curr_ltr != '\'')
{
data = 0;
emit_intermediate(curr_start, (void *)1, &data - curr_start + 1);
state = NOT_IN_WORD;
}
break;
default:
case NOT_IN_WORD:
if (curr_ltr >= 'A' && curr_ltr <= 'Z')
{
curr_start = &data;
data = curr_ltr;
state = IN_WORD;
}
break;
}
}
// Add the last word
if (state == IN_WORD)
{
data[args->length] = 0;
emit_intermediate(curr_start, (void *)1, &data - curr_start + 1);
}
}
/** wordcount_reduce()
* 计算字符
*/
void wordcount_reduce(void *key_in, iterator_t *itr)
{
char *key = (char *)key_in;
void *val;
intptr_t sum = 0;
assert(key);
assert(itr);
while (iter_next (itr, &val))
{
sum += (intptr_t)val;
}
emit(key, (void *)sum);
}
void *wordcount_combiner (iterator_t *itr)
{
void *val;
intptr_t sum = 0;
assert(itr);
while (iter_next (itr, &val))
{
sum += (intptr_t)val;
}
return (void *)sum;
}
int main(int argc, char *argv[])
{
final_data_t wc_vals;
int i;
int fd;
char * fdata;
int disp_num;
struct stat finfo;
char * fname, * disp_num_str;
struct timeval starttime,endtime;
get_time (&begin);
// 确保文件名
if (argv[1] == NULL)
{
printf("USAGE: %s <filename> [Top # of results to display]\n", argv[0]);
exit(1);
}
fname = argv[1];
disp_num_str = argv[2];
printf("Wordcount: Running...\n");
// 读取文件
CHECK_ERROR((fd = open(fname, O_RDONLY)) < 0);
// Get the file info (for file length)
CHECK_ERROR(fstat(fd, &finfo) < 0);
#ifndef NO_MMAP
// 内存里面开始调用 map
CHECK_ERROR((fdata = mmap(0, finfo.st_size + 1,
PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) == NULL);
#else
int ret;
fdata = (char *)malloc (finfo.st_size);
CHECK_ERROR (fdata == NULL);
ret = read (fd, fdata, finfo.st_size);
CHECK_ERROR (ret != finfo.st_size);
#endif
CHECK_ERROR((disp_num = (disp_num_str == NULL) ?
DEFAULT_DISP_NUM : atoi(disp_num_str)) <= 0);
wc_data_t wc_data;
wc_data.unit_size = 5; // approx 5 bytes per word
wc_data.fpos = 0;
wc_data.flen = finfo.st_size;
wc_data.fdata = fdata;
CHECK_ERROR (map_reduce_init ());
map_reduce_args_t map_reduce_args;
memset(&map_reduce_args, 0, sizeof(map_reduce_args_t));
map_reduce_args.task_data = &wc_data;
map_reduce_args.map = wordcount_map;
map_reduce_args.reduce = wordcount_reduce;
map_reduce_args.combiner = wordcount_combiner;
map_reduce_args.splitter = wordcount_splitter;
map_reduce_args.locator = wordcount_locator;
map_reduce_args.key_cmp = mystrcmp;
map_reduce_args.unit_size = wc_data.unit_size;
map_reduce_args.partition = NULL; // use default
map_reduce_args.result = &wc_vals;
map_reduce_args.data_size = finfo.st_size;
map_reduce_args.L1_cache_size = atoi(GETENV("MR_L1CACHESIZE"));//1024 * 1024 * 2;
map_reduce_args.num_map_threads = atoi(GETENV("MR_NUMTHREADS"));//8;
map_reduce_args.num_reduce_threads = atoi(GETENV("MR_NUMTHREADS"));//16;
map_reduce_args.num_merge_threads = atoi(GETENV("MR_NUMTHREADS"));//8;
map_reduce_args.num_procs = atoi(GETENV("MR_NUMPROCS"));//16;
map_reduce_args.key_match_factor = (float)atof(GETENV("MR_KEYMATCHFACTOR"));//2;
printf("Wordcount: Calling MapReduce Scheduler Wordcount\n");
gettimeofday(&starttime,0);
get_time (&end);
#ifdef TIMING
fprintf (stderr, "initialize: %u\n", time_diff (&end, &begin));
#endif
get_time (&begin);
CHECK_ERROR(map_reduce (&map_reduce_args) < 0);
get_time (&end);
#ifdef TIMING
library_time += time_diff (&end, &begin);
#endif
get_time (&begin);
gettimeofday(&endtime,0);
printf("Wordcount: Completed %ld\n",(endtime.tv_sec - starttime.tv_sec));
printf("Wordcount: MapReduce Completed\n");
printf("Wordcount: Calling MapReduce Scheduler Sort\n");
mapreduce_sort(wc_vals.data, wc_vals.length, sizeof(keyval_t), mykeyvalcmp);
CHECK_ERROR (map_reduce_finalize ());
printf("Wordcount: MapReduce Completed\n");
dprintf("\nWordcount: Results (TOP %d):\n", disp_num);
for (i = 0; i < disp_num && i < wc_vals.length; i++)
{
keyval_t * curr = &((keyval_t *)wc_vals.data);
dprintf("%15s - %" PRIdPTR "\n", (char *)curr->key, (intptr_t)curr->val);
}
free(wc_vals.data);
#ifndef NO_MMAP
CHECK_ERROR(munmap(fdata, finfo.st_size + 1) < 0);
#else
free (fdata);
#endif
CHECK_ERROR(close(fd) < 0);
get_time (&end);
#ifdef TIMING
fprintf (stderr, "finalize: %u\n", time_diff (&end, &begin));
#endif
return 0;
}

返回列表