-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.html
114 lines (104 loc) · 12 KB
/
README.html
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
<h1 id="map-reduce">Map Reduce</h1>
<p>In 2004, engineers at Google introduced a new paradigm for large-scale parallel data processing known as MapReduce. One key aspect of MapReduce is that it makes programming such tasks on large-scale clusters easy for developers; instead of worrying about how to manage parallelism, handle machine crashes, and many other complexities common within clusters of machines, the developer can instead just focus on writing little bits of code (described below) and the infrastructure handles the rest.</p>
<p>In this project, you'll be building a simplified version of MapReduce for just a single machine. While somewhat easier to build MapReduce for a single machine, there are still numerous challenges, mostly in building the correct concurrency support. Thus, you'll have to think a bit about how to build the MapReduce implementation, and then build it to work efficiently and correctly.</p>
<p>There are three specific objectives to this assignment:</p>
<ul>
<li>To learn about the general nature of the MapReduce paradigm.</li>
<li>To implement a correct and efficient MapReduce framework using threads and related functions.</li>
<li>To gain more experience writing concurrent code.</li>
</ul>
<h2 id="general-idea">General Idea</h2>
<p>Let's now get into the exact code you'll have to build. The MapReduce infrastructure you will build supports the execution of user-defined <code>Map()</code> and <code>Reduce()</code> functions.</p>
<p>As from the original paper: "<code>Map()</code>, written by the user, takes an input pair and produces a set of intermediate key/value pairs. The MapReduce library groups together all intermediate values associated with the same intermediate key K and passes them to the <code>Reduce()</code> function."</p>
<p>"The <code>Reduce()</code> function, also written by the user, accepts an intermediate key K and a set of values for that key. It merges together these values to form a possibly smaller set of values; typically just zero or one output value is produced per <code>Reduce()</code> invocation. The intermediate values are supplied to the user's reduce function via an iterator."</p>
<p>A classic example, written here in pseudocode, shows how to count the number of occurrences of each word in a set of documents:</p>
<pre><code>map(String key, String value):
// key: document name
// value: document contents
for each word w in value:
EmitIntermediate(w, "1");
reduce(String key, Iterator values):
// key: a word
// values: a list of counts
int result = 0;
for each v in values:
result += ParseInt(v);
print key, result;</code></pre>
<p>What's fascinating about MapReduce is that so many different kinds of relevant computations can be mapped onto this framework. The original paper lists many examples, including word counting (as above), a distributed grep, a URL frequency access counters, a reverse web-link graph application, a term-vector per host analysis, and others.</p>
<p>What's also quite interesting is how easy it is to parallelize: many mappers can be running at the same time, and later, many reducers can be running at the same time. Users don't have to worry about how to parallelize their application; rather, they just write <code>Map()</code> and <code>Reduce()</code> functions and the infrastructure does the rest.</p>
<h2 id="code-overview">Code Overview</h2>
<p>We give you here the <a href="https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-mapreduce/mapreduce.h"><code>mapreduce.h</code></a> header file that specifies exactly what you must build in your MapReduce library:</p>
<pre><code>#ifndef __mapreduce_h__
#define __mapreduce_h__
// Different function pointer types used by MR
typedef char *(*Getter)(char *key, int partition_number);
typedef void (*Mapper)(char *file_name);
typedef void (*Reducer)(char *key, Getter get_func, int partition_number);
typedef unsigned long (*Partitioner)(char *key, int num_partitions);
// External functions: these are what you must define
void MR_Emit(char *key, char *value);
unsigned long MR_DefaultHashPartition(char *key, int num_partitions);
void MR_Run(int argc, char *argv[],
Mapper map, int num_mappers,
Reducer reduce, int num_reducers,
Partitioner partition);
#endif // __mapreduce_h__</code></pre>
<p>The most important function is <code>MR_Run</code>, which takes the command line parameters of a given program, a pointer to a Map function (type <code>Mapper</code>, called <code>map</code>), the number of mapper threads your library should create (<code>num_mappers</code>), a pointer to a Reduce function (type <code>Reducer</code>, called <code>reduce</code>), the number of reducers (<code>num_reducers</code>), and finally, a pointer to a Partition function (<code>partition</code>, described below).</p>
<p>Thus, when a user is writing a MapReduce computation with your library, they will implement a Map function, implement a Reduce function, possibly implement a Partition function, and then call <code>MR_Run()</code>. The infrastructure will then create threads as appropriate and run the computation.</p>
<p>One basic assumption is that the library will create <code>num_mappers</code> threads (in a thread pool) that perform the map tasks. Another is that your library will create <code>num_reducers</code> threads to perform the reduction tasks. Finally, your library will create some kind of internal data structure to pass keys and values from mappers to reducers; more on this below.</p>
<h2 id="simple-example-wordcount">Simple Example: Wordcount</h2>
<p>Here is a simple (but functional) wordcount program, written to use this infrastructure:</p>
<pre><code>#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mapreduce.h"
void Map(char *file_name) {
FILE *fp = fopen(file_name, "r");
assert(fp != NULL);
char *line = NULL;
size_t size = 0;
while (getline(&line, &size, fp) != -1) {
char *token, *dummy = line;
while ((token = strsep(&dummy, " \t\n\r")) != NULL) {
MR_Emit(token, "1");
}
}
free(line);
fclose(fp);
}
void Reduce(char *key, Getter get_next, int partition_number) {
int count = 0;
char *value;
while ((value = get_next(key, partition_number)) != NULL)
count++;
printf("%s %d\n", key, count);
}
int main(int argc, char *argv[]) {
MR_Run(argc, argv, Map, 10, Reduce, 10, MR_DefaultHashPartition);
}</code></pre>
<p>Let's walk through this code, in order to see what it is doing. First, notice that <code>Map()</code> is called with a file name. In general, we assume that this type of computation is being run over many files; each invocation of <code>Map()</code> is thus handed one file name and is expected to process that file in its entirety.</p>
<p>In this example, the code above just reads through the file, one line at a time, and uses <code>strsep()</code> to chop the line into tokens. Each token is then emitted using the <code>MR_Emit()</code> function, which takes two strings as input: a key and a value. The key here is the word itself, and the token is just a count, in this case, 1 (as a string). It then closes the file.</p>
<p>The <code>MR_Emit()</code> function is thus another key part of your library; it needs to take key/value pairs from the many different mappers and store them in a way that later reducers can access them, given constraints described below. Designing and implementing this data structure is thus a central challenge of the project.</p>
<p>After the mappers are finished, your library should have stored the key/value pairs in such a way that the <code>Reduce()</code> function can be called. <code>Reduce()</code> is invoked once per key, and is passed the key along with a function that enables iteration over all of the values that produced that same key. To iterate, the code just calls <code>get_next()</code> repeatedly until a NULL value is returned; <code>get_next</code> returns a pointer to the value passed in by the <code>MR_Emit()</code> function above, or NULL when the key's values have been processed. The output, in the example, is just a count of how many times a given word has appeared, and is just printed to standard output.</p>
<p>All of this computation is started off by a call to <code>MR_Run()</code> in the <code>main()</code> routine of the user program. This function is passed the <code>argv</code> array, and assumes that <code>argv[1]</code> ... <code>argv[n-1]</code> (with <code>argc</code> equal to <code>n</code>) all contain file names that will be passed to the mappers.</p>
<p>One interesting function that you also need to pass to <code>MR_Run()</code> is the partitioning function. In most cases, programs will use the default function (<code>MR_DefaultHashPartition</code>), which should be implemented by your code. Here is its implementation:</p>
<pre><code>unsigned long MR_DefaultHashPartition(char *key, int num_partitions) {
unsigned long hash = 5381;
int c;
while ((c = *key++) != '\0')
hash = hash * 33 + c;
return hash % num_partitions;
}</code></pre>
<p>The function's role is to take a given <code>key</code> and map it to a number, from <code>0</code> to <code>num_partitions - 1</code>. Its use is internal to the MapReduce library, but critical. Specifically, your MR library should use this function to decide which partition (and hence, which reducer thread) gets a particular key/list of values to process. For some applications, which reducer thread processes a particular key is not important (and thus the default function above should be passed in to <code>MR_Run()</code>); for others, it is, and this is why the user can pass in their own partitioning function as need be.</p>
<p>One last requirement: For each partition, keys (and the value list associated with said keys) should be <strong>sorted</strong> in ascending key order; thus, when a particular reducer thread (and its associated partition) are working, the <code>Reduce()</code> function should be called on each key in order for that partition.</p>
<h2 id="considerations">Considerations</h2>
<p>Here are a few things to consider in your implementation:</p>
<ul>
<li><p><strong>Thread Management</strong>. This part is fairly straightforward. You should create <code>num_mappers</code> mapping threads, and assign a file to each <code>Map()</code> invocation in some manner you think is best (e.g., Round Robin, Shortest-File-First, etc.). Which way might lead to best performance? You should also create <code>num_reducers</code> reducer threads at some point, to work on the map'd output.</p></li>
<li><p><strong>Partitioning and Sorting</strong>. Your central data structure should be concurrent, allowing mappers to each put values into different partitions correctly and efficiently. Once the mappers have completed, a sorting phase should order the key/value-lists. Then, finally, each reducer thread should start calling the user-defined <code>Reduce()</code> function on the keys in sorted order per partition. You should think about what type of locking is needed throughout this process for correctness.</p></li>
<li><p><strong>Memory Management</strong>. One last concern is memory management. The <code>MR_Emit()</code> function is passed a key/value pair; it is the responsibility of the MR library to make copies of each of these. Then, when the entire mapping and reduction is complete, it is the responsibility of the MR library to free everything.</p></li>
</ul>
<h2 id="grading">Grading</h2>
<p>Your code should turn in <code>mapreduce.c</code> which implements the above functions correctly and efficiently. It will be compiled with test applications with the <code>-Wall -Werror -pthread -O</code> flags; it will also be valgrinded to check for memory errors.</p>
<p>Your code will first be measured for correctness, ensuring that it performs the maps and reductions correctly. If you pass the correctness tests, your code will be tested for performance; higher performance will lead to better scores.</p>