Xorkox Xorkox - 1 year ago 92
C++ Question

OpenMP: Upper text to lower

I try to do a task for university. There we need to take large Text from std::cin and convert every character with 'tolower()' to a lower char and put it into std::cout.

This should be paralellized and scalable, first I thought about spliting the text in lines and give every line to a new thread but the task should be done exclusively with openMP. And there should only be so much threads used, as my machine can run parallel, right?

So there is this openMP 'for' paradigm:

#pragma omp parallel for

Is it a good Idea, to iterate over every single char in a for loop? Or is there a better way?

Thanks for your advice!

Answer Source

You're going to iterate over every single char in any case. When it comes to OpenMP, however, it already chooses a number of threads based on your machine and a chunk strategy over your iterations; either is tunable, but you likely won't need to. The main restriction in this case is actually that you're instructed to use tolower.

One note: Splitting into lines is not easily parallelized. If your input comes from a terminal, for instance, it's already done for you by the terminal layer if you do non-blocking reads or line based reads (mostly; it's possible to send multiple lines at once). If you read from a file, splitting into lines requires scanning the whole file for newlines and calculating where they start and end. But that's completely wasted effort since your operation doesn't depend on the lines; tolower doesn't change newline characters, and doesn't add or remove characters.

As for whether it's a good idea to use a for loop; in OpenMP C, yes it is. OpenMP translates your typical for (i=0; i<len; i++) into a smarter algorithm with #pragma omp parallel for. With a higher level language, the operation you need is called map, and may have a parallel version such as Haskell's parMap. In such a language you basically do output = map tolower input. I expect someone with modern C++ knowledge will chime in with some Boost template to do it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download