Suppose I also need the index of a minimal element; is there any way to use the abbreviation clause for this?
Unfortunately not. the list of possible abbreviations in OpenMP is very ... small. In particular, min
and max
are the only "higher level" functions and are customizable. For everyone.
I must admit that I do not like the OpenMPs approach to abbreviations, precisely because it does not expand in the slightest degree, it is intended only for work in special cases. Of course, these are interesting special cases, but still a fundamentally bad approach.
For such operations, you need to implement the abbreviation yourself by copying the local stream results into local stream variables and combining them at the end.
The easiest way to do this (and even very close to how OpenMP implements abbreviations) is to have an array with elements for each thread and use omp_get_thread_num()
to access the element. Note, however, that this will lead to performance degradation due to a false exchange if the elements of the array share the cache line. To reduce this, apply an array:
struct min_element_t { double min_val; size_t min_index; }; size_t const CACHE_LINE_SIZE = 1024;
source share