92
edits
Changes
→Case B - Using the Parallel Stacks Window
}
==Case B - Using the Parallel Stacks and the Parallel Watch Window==
We will use the following program, which uses cilk API for parallelization, to experiment with the Parallel Stacks and the Parallel Watch Window:
// cilk threadscilkthreads.cpp
#include <iostream>
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
int main() {
int nwt = __cilkrts_get_nworkers();
std::cout << "Number of workers is " << nwt << std::endl;
int a i = 1;
cilk_spawn foo(i); cilk_spawn coo(i); cilk_spawn boo(i); cilk_spawn doo(i); cilk_spawn zoo(i);
foo(i);
cilk_sync;
}
int tid = __cilkrts_get_worker_number();
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("Foo! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("Boo! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("Coo! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("Doo! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("Zoo! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
printf("Bla bla! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
printf("Blo Blo! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
printf("Blu Blu! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
printf("Vroom! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
printf("Beep beep! from worker %d\n", tid);
}
printf("Screeeeeeeeechhhhhh! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
}
int tid = __cilkrts_get_worker_number();
printf("Meow! from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
printf("Oink! from worker %d\n", tid);
returni;
}
int tid = __cilkrts_get_worker_number();
for (int i = 0; i < 10; i++) {
cough(i);
}
printf("Zzzzzzzzz...... from worker %d\n", tid);
}
int tid = __cilkrts_get_worker_number();
}
In the above code, the main program calls functions that may themselves call other functions. At each cilk_spawn keyword, we can expect a new child thread to call the function. However, if the function have very short operations each, then the different spawns may not even be distributed to different child threads, since each function call may take very fast. That was originally the case, where all of the function calls were done by one thread. Therefore, the functions were adjusted to sleep for 1 second within the function itself. This way, the functions took long enough so that the program did spawn into multiple child threads.