-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadedSync.tex
143 lines (133 loc) · 3.73 KB
/
threadedSync.tex
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
\begin{frame}[fragile]
\frametitle{\code{sync} methods}
\begin{itemize}
\item Synchronous as opposed to asynchronous
\item They return a value - always a \code{message} type
\item Other than that, just like any other entry method:
\end{itemize}
In the interface file:
\begin{lstlisting}
entry [sync] MsgData * f(double A[2*m], int m );
\end{lstlisting}
In the C++ file:
\begin{lstlisting}
MsgData *f(double X[], int size) {
...
m = new MsgData(..);
...
return m;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{How to invoke it?}
\begin{itemize}
\item We would just like to invoke a sync method normally:
\begin{lstlisting}
MsgData * m = A[i].foo(.. parameters..);
\end{lstlisting}
\item Do you see any problem with this?
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Threaded methods}
\begin{itemize}
\item Any method that calls a sync method must be able to suspend:
\begin{itemize}
\item Needs to be declared as a \code{threaded} method
\item A threaded method of a chare C
\begin{itemize}
\item Can suspend, without blocking the processor
\item Other chares can then be executed
\item Even other methods of chare C can be executed
\end{itemize}
\end{itemize}
\end{itemize}
\end{frame}
% \begin{frame}[fragile]
% \frametitle{A complete example}
% \begin{itemize}
% \item A chare array A of N elements, and each element holds a single number
% \item Check if the numbers are already in a sorted order?
% \item Each chare checks with its right neighbor, in parallel, and combine there
% results via a reduction
% \end{itemize}
% \end{frame}
% \begin{frame}[fragile]
% \frametitle{Interface File}
% \begin{lstlisting}
% mainmodule arrayRing {
% message MsgData;
% readonly int numElements;
% mainchare Main {
% entry Main();
% entry void allFinished(CkReductionMsg *m);
% };
% array [1D] SimpleArray {
% entry SimpleArray();
% entry [threaded] void run();
% entry [sync] MsgData * blockingInvocation(int i);
% };
% }
% \end{lstlisting}
% \end{frame}
% \begin{frame}[fragile]
% \frametitle{Main class}
% \begin{lstlisting}[basicstyle=\scriptsize]
% class Main : public CBase_Main {
% public:
% Main(CkMigrateMessage *msg) { }
% Main(CkArgMsg* msg) {
% numElements = 10;
% if (msg->argc > 1) numElements = atoi(msg->argv[1]);
% CProxy_SimpleArray elems = CProxy_SimpleArray::ckNew(numElements);
% CkCallback *cb = new CkCallback(CkIndex_Main::allFinished(NULL), thisProxy);
% elems.ckSetReductionClient(cb);
% elems.run();
% }
% void allFinished(CkReductionMsg *m) {
% int *sorted = (int *) m->getData();
% if (*sorted == numElements)
% CkPrintf("Elements were sorted\n");
% else
% CkPrintf("Elements were not sorted\n");
% CkExit();
% }
% };
% \end{lstlisting}
% \end{frame}
% \begin{frame}[fragile]
% \frametitle{MsgData class}
% \begin{lstlisting}
% struct MsgData: public CMessage_MsgData {
% double value;
% };
% \end{lstlisting}
% \end{frame}
% \begin{frame}[fragile]
% \frametitle{SimpleArray class}
% \begin{lstlisting}[basicstyle=\scriptsize]
% class SimpleArray : public CBase_SimpleArray {
% private: double myValue;
% public:
% SimpleArray(CkMigrateMessage *msg) { }
% SimpleArray() {
% myValue = drand48();
% }
% inline int nextI() { return ((thisIndex + 1) % numElements); }
% void run() {
% int contrib = 1;
% if (thisIndex > nextI()) {
% MsgData *m = thisProxy(nextI()).blockingInvocation(thisIndex);
% if(myValue > m->value) contrib = 0;
% }
% contribute(sizeof(int), &contrib, CkReduction::sum_int);
% }
% MsgData* blockingInvocation(int i) {
% MsgData * m = new MsgData();
% m->value = myValue;
% return m;
% }
% };
% \end{lstlisting}
% \end{frame}