OpenMP preprocessor macro, omp_get_num_threads and test.cpp

15 views
Skip to first unread message

Jeffrey Walton

unread,
Jun 12, 2015, 9:20:19 AM6/12/15
to cryptop...@googlegroups.com
Does anyone see any problems with the following patch?

I found it useful during benchmarking (because I was not getting multiple OMP threads like I expected). if you add `-fopenmp` to CXXFLAGS, here's the output it produces:

    $ ./cryptest.exe v
    Using seed: 1434115590
    Using 1 OMP threads

    Testing Settings...

if there are no issues, I'd like to consider it for the library as a patch.

Jeff

$ git diff test.cpp
diff --git a/test.cpp b/test.cpp
index 17d4909..9066b2e 100644
--- a/test.cpp
+++ b/test.cpp
@@ -47,6 +47,10 @@
 #pragma comment(lib, "ws2_32.lib")
 #endif
 
+#if defined(_OPENMP) && (_OPENMP > 0)
+# include <omp.h>
+#endif
+
 USING_NAMESPACE(CryptoPP)
 USING_NAMESPACE(std)
 
@@ -769,9 +773,15 @@ bool Validate(int alg, bool thorough, const char *seedInput
        std::string seed = seedInput ? std::string(seedInput) : IntToString(time
        seed.resize(16);
 
-       cout << "Using seed: " << seed << endl << endl;
+       cout << "Using seed: " << seed << endl;
        s_globalRNG.SetKeyWithIV((byte *)seed.data(), 16, (byte *)seed.data());
 
+#if defined(_OPENMP) && (_OPENMP > 0)
+       cout << "Using " << omp_get_num_threads() << " OMP threads" << endl;
+#endif
+
+       cout << endl;
+
        switch (alg)
        {
        case 0: result = ValidateAll(thorough); break;

Jean-Pierre Münch

unread,
Jun 12, 2015, 11:38:30 AM6/12/15
to cryptop...@googlegroups.com
Hey Jeff,

so you're formulating it so that the user needs to explicetly activate OMP using the settings (makefile / vcxproj file)?
If the user doesn't run into any problems if he doesn't have OMP it's fine for me.

Did you check if VisualStudio provides the _OPENMP macro or if there's a different one used?
In either case you might want to consider making some CRYPTOPP_OPENMP_AVAILABLE in config.h, so we can change the behavior at one single point.

BR

JPM
--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to cryptopp-user...@googlegroups.com.
More information about Crypto++ and this group is available at http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeffrey Walton

unread,
Jun 13, 2015, 5:05:25 PM6/13/15
to cryptop...@googlegroups.com

so you're formulating it so that the user needs to explicetly activate OMP using the settings (makefile / vcxproj file)?

Doing something to activate OMP is existing behavior. That is not changing.

For example, on Linux, I need to add -fopenmp flag to CXXFLAGS. On Windows, I'm not sure what I have to do.

If the user doesn't run into any problems if he doesn't have OMP it's fine for me.

Yep, preserve existing behavior is an underlying theme. If the user does nothing, then nothing new happens.

Did you check if VisualStudio provides the _OPENMP macro or if there's a different one used?

On Linux, -fopenmp defines _OPENMP (http://stackoverflow.com/q/30803126).

On Windows, _OPENMP is not defined, so nothing changes. I'm not sure what needs to be done because I don't use OpenMP on Windows.

In either case you might want to consider making some CRYPTOPP_OPENMP_AVAILABLE in config.h, so we can change the behavior at one single point.

Well, I think we can pick it up automatically based on user actions. If the user can activate OMP in one step (-fopenmp), then I don't think there's a reason to require two steps (-fopenmp and CRYPTOPP_OPENMP_AVAILABLE).

Also, OpenMP is incorporated with instrumentation though #pragmas, so I don't think there's much point in conditionally removing them. For example, exiting code:

    #pragma omp parallel
        #pragma omp sections
        {
            #pragma omp section
                cp = ModularSquareRoot(cp, m_p);
            #pragma omp section
                cq = ModularSquareRoot(cq, m_q);
        }

In the code above, the ModularSquareRoot always executes. Guarding code blocks with CRYPTOPP_OPENMP_AVAILABLE won't gain anything, and it makes us write more code:

#ifdef CRYPTOPP_OPENMP_AVAILABLE
    #pragma omp parallel
        #pragma omp sections
        {
            #pragma omp section
                cp = ModularSquareRoot(cp, m_p);
            #pragma omp section
                cq = ModularSquareRoot(cq, m_q);
        }
#else
    cp = ModularSquareRoot(cp, m_p);
    cq = ModularSquareRoot(cq, m_q);
#endif
Reply all
Reply to author
Forward
0 new messages