<div dir="ltr">Hi,<div><br></div><div>Alexey Bataev and I (Lingda Li) would like to have your attention on an ongoing discussion of 2 schemes to implement the declare mapper in OpenMP 5.0. The detailed discussion can be found at <a href="https://reviews.llvm.org/D59474" target="_blank">https://reviews.llvm.org/D59474</a></div><div><br></div><div>Scheme 1 (the one has been implemented by me in <a href="https://reviews.llvm.org/D59474" target="_blank">https://reviews.llvm.org/D59474</a>):</div><div>The detailed design can be found at <a href="https://github.com/lingda-li/public-sharing/blob/master/mapper_runtime_design.pptx">https://github.com/lingda-li/public-sharing/blob/master/mapper_runtime_design.pptx</a></div><div>For each mapper function, the compiler generates a function like this:</div><div><br></div><div>```</div><div>void <type>.mapper(void *base, void *begin, size_t size, int64_t type) {<br>  // Allocate space for an array section first.<br>  if (size > 1 && !maptype.IsDelete)<br>     <push>(base, begin, size*sizeof(Ty), clearToFrom(type));<br>   <br>  // Map members.<br>  for (unsigned i = 0; i < size; i++) {<br>     // For each component specified by this mapper:<br>     for (auto c : components) {</div><div>       ...; // code to generate c.arg_base, c.arg_begin, c.arg_size, c.arg_type<br>       if (c.hasMapper())<br>         (*c.Mapper())(c.arg_base, c.arg_begin, c.arg_size, c.arg_type);<br>       else<br>         <push>(c.arg_base, c.arg_begin, c.arg_size, c.arg_type);<br>     }<br>  }<br>  // Delete the array section.<br>  if (size > 1 && maptype.IsDelete)<br>    <push>(base, begin, size*sizeof(Ty), clearToFrom(type));<br>}<br></div><div>```</div><div>This function is passed to the OpenMP runtime, and the runtime will call this function to finish the data mapping.</div><div><br></div><div><br></div><div>Scheme 2 (which Alexey proposes):</div><div>Alexey proposes to move parts of the mapper function above into the OpenMP runtime, so the compiler will generate code below:</div><div>```</div><div>void <type>.mapper(void *base, void *begin, size_t size, int64_t type) {</div><div>  ...; // code to generate arg_base, arg_begin, arg_size, arg_type, arg_mapper.</div> auto sub_components[] = {...}; // fill in generated begin, base, ...<br> __tgt_mapper(base, begin, size, type, sub_components);<br>}<div>```</div><div><br></div><div>`__tgt_mapper` is a runtime function as below:</div><div>```</div><div>void __tgt_mapper(void *base, void *begin, size_t size, int64_t type, auto components[]) {<br>  // Allocate space for an array section first.<br>  if (size > 1 && !maptype.IsDelete)<br>     <push>(base, begin, size*sizeof(Ty), clearToFrom(type));<br>   <br>  // Map members.<br>  for (unsigned i = 0; i < size; i++) {<br>     // For each component specified by this mapper:<br>     for (auto c : components) {<br>       if (c.hasMapper())<br>         (*c.Mapper())(c.arg_base, c.arg_begin, c.arg_size, c.arg_type);<br>       else<br>         <push>(c.arg_base, c.arg_begin, c.arg_size, c.arg_type);<br>     }<br>  }<br>  // Delete the array section.<br>  if (size > 1 && maptype.IsDelete)<br>    <push>(base, begin, size*sizeof(Ty), clearToFrom(type));<br>}<br></div><div>```</div><div><br></div><div>Comparison:</div><div>Why to choose 1 over 2:</div><div>1. In scheme 2,  the compiler needs to generate all map types and pass them to __tgt_mapper through sub_components. But in this case, the compiler won't be able to generate the correct MEMBER_OF field in map type. As a result, the runtime has to fix it using the mechanism we already have here: __tgt_mapper_num_components. This not only increases complexity, but also, it means the runtime needs further manipulation of the map type, which creates locality issues. While in the current scheme, the map type is generated by compiler once, so the data locality will be very good in this case.</div><div>2. In scheme 2, sub_components includes all components that should be mapped. If we are mapping an array, this means we need to map many components, which will need to allocate memory for sub_components in the heap. This creates further memory management burden and is not an efficient way to use memory.</div><div>3. In scheme 1, we are able to inline nested mapper functions. As a result, the compiler can do further optimizations to optimize the mapper function, e.g., eliminate redundant computation, loop unrolling, and thus achieve potentially better performance. We cannot achieve these optimizations in scheme 2.</div><div><br></div><div>Why to choose 2 over 1:</div><div>1. Less code in the mapper function codegen (I doubt this because the codegen function of scheme 1 uses less than 200 loc)</div><div><br></div><div><br></div><div>We will appreciate if you can share your opinions.</div><div><br></div><div>Thanks,</div><div>Lingda Li</div></div>