Software Archive
Read-only legacy content
17061 Discussions

Deleting a two dimensional reducer

leifblaese
Beginner
330 Views

Hi,

i have in my program a list of vectors, the vectors contain integer. I create this list via the 'new' keyword. To fill this list in parallel, i changed the list to a cilk::reducer_list_append. So far so good, everything works fine. 

 

My question is: How do i properly delete this reducer-list? If it was a 'normal' two dimensional list, i would loop through the list, delete everything vector or just pop_back() until the list is empty and then delete the list itself. But i can't do that with a reducer, can i?  Is there a way to access e.g. the first element to delete it or is there something similar to the pop_back() oder pop_front()-function? Or is it enough to 'just delete' the list?

[cpp]

#include <cilk/reducer_list.h>

#include <vector>

int main() {
cilk::reducer_list_append< vector< int >  > * mylist = new cilk::reducer_list_append< vector< int > ;
        vector < int > myvector (4,1);
        mylist.push_back(myvector);
        mylist.push_back(myvector);
        delete(mylist) //What exactly happens here?

}

[/cpp]

0 Kudos
2 Replies
Barry_T_Intel
Employee
330 Views
Following through all of the nested, templated classes can be confusing, to say the least. Adding in a trip or two through runtime routines make it even harder to follow. A quick review of reducers: Associated with each worker is a map of reducers. When you create a new reducer, it is added to the current worker's reducer map. When a steal occurs, the thief get a new, empty reducer map. The first time a reducer is referenced, an identity for the reducer is created and added to that worker's reducer map. When a sync occurs, the reducer maps are combined so that the values maintain their serial ordering. This is why reducers must be associative. When two reducer maps are combined, the "right" map is merged into the "left" map, and any elements that are no longer needed are destructed. "Right" and "left" refers to their order in a Directed Acyclic Graph of the application, where "left" would occur first in a serial execution. Back to how destruction works: A reducer_list_append contains a reducer, imp_. The reducer class is defined in reducer.h. Among other things, it provides a method called view() which provides access to the per-strand object of type std::list<_Ty, _Ax>. _Ty is the element type and _Ax is the allocator you specified when you defined the reducer_list_append. So when you delete the reducer_list_append my_list, the default destructor will destruct imp_. The destructor for imp_ will call __cilkrts_hyper_destroy(), which will remove the reducer from the reducer map and call the Monoid's destroy method, which will destruct the underlying object. Which for a reducer_list_append is your std::list. The destructor for std::list will in turn destruct each of the elements in the list. So I'd expect that when the std::list destructs the two vectors, they'd do what you'd expect. - Barry
0 Kudos
leifblaese
Beginner
330 Views
Thank you, that helped me very much.
0 Kudos
Reply