Logo

dev-resources.site

for different kinds of informations.

How to use OpenMP to parallelize C++ using Visual studio

Published at
12/1/2024
Categories
cpp
openmp
visualstudio
tutorial
Author
tuttelikz
Categories
4 categories in total
cpp
open
openmp
open
visualstudio
open
tutorial
open
Author
9 person written this
tuttelikz
open
How to use OpenMP to parallelize C++ using Visual studio

Openmp
Parallel programming is the process of breaking down a large task into smaller sub-tasks that can be executed simultaneously, thus utilizing the available computing resources more efficiently. OpenMP is a widely used API for parallel programming in C++. It allows developers to write parallel code easily and efficiently by adding simple compiler directives to their existing code.

The OpenMP C++ application program interface lets us write applications that effectively use multiple processors. In this tutorial, we will cover how to run a parallel code in Visual C++, which supports the OpenMP 2.0 standard.

Outline

  1. Configure C++ project
    • Create a new C++ project
    • Enable OpenMP support
  2. Run code
    • Add code for parallel loop and Run

Source code: https://github.com/tuttelikz/notes/tree/main/openmp-vs/OpenMPApp


Configure CPP project

Create a new cpp project

Open Visual Studio > Create a new project > Set "C++", "Windows" and "Console" filters to find the template > click Next

Create a new cpp project

Enable OpenMP support

Navigate to Project > Properties
Project properties

Then C/C++ > Language > Open MP Support > Choose "Yes" > Apply > OK

Enable OpenMP support

Importanly, make sure precompiled headers are off by Precompiled Headers > Precompiled Header > Not Using Precompiled Headers > Apply > OK

No precompiled compilers


Run code

Add code to OpenMPApp.cpp and Run

To test OpenMP functionality in C++ project, we defined a simple sum function which calculates result using conventional serial loop and parallel loop. Add the following code to OpenMPApp.cpp

#include <chrono> 
#include <iostream> 
#include <omp.h>

using namespace std;

// Serial for loop
int sum_serial(int n)
{
    int sum = 0;
    for (int i = 0; i <= n; ++i) {
        sum += i;
    }
    cout << "Serial result: " << sum
        << endl;

    return sum;
}

// Parallel for loop
int sum_parallel(int n)
{
    int sum = 0;
#pragma omp parallel for reduction(+ : sum) 
    for (int i = 0; i <= n; ++i) {
        sum += i;
    }
    cout << "Parallel result: " << sum
        << endl;

    return sum;
}

int main()
{
    int num_threads = 8;
    omp_set_num_threads(num_threads);

    const int n = 100000000;

    auto start_time
        = chrono::high_resolution_clock::now();
    int result_serial = sum_serial(n);
    auto end_time
        = chrono::high_resolution_clock::now();
    chrono::duration<double> serial_duration = end_time - start_time;

    start_time = chrono::high_resolution_clock::now();
    int result_parallel = sum_parallel(n);
    end_time = chrono::high_resolution_clock::now();
    chrono::duration<double> parallel_duration = end_time - start_time;

    cout << "Serial time elapsed: "
        << serial_duration.count() << " seconds"
        << endl;
    cout << "Parallel time elapsed: "
        << parallel_duration.count() << " seconds"
        << endl;
    cout << "Speedup for " << num_threads << " threads: "
        << serial_duration.count()
        / parallel_duration.count()
        << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Press Start debugging to see the result
Start debugging


Thanks for reading! Stay tuned for more content, and feel free to share your thoughts and feedback! Your reactions help me improve and create even more useful posts πŸ™‚

Alternate URL: https://github.com/tuttelikz/notes/tree/main/openmp-vs

visualstudio Article's
30 articles in total
Favicon
Can Skater's Secure Depot of Private Keys be used as a tool designed for securely storing and managing private keys?
Favicon
Code Protection
Favicon
How to use OpenMP to parallelize C++ using Visual studio
Favicon
Getting started with .Net Aspire
Favicon
Integrating OpenCV with Visual Studio C++
Favicon
Version 9 of .Net is here
Favicon
EasyTdd 0.5.0: Streamlining Mocking with Incremental FluentMock
Favicon
From Legacy to .NET 8: Migrating with NDepend
Favicon
LocalDb Dev on Windows11 ARM
Favicon
How to create custom snippets in Visual Studio 2022
Favicon
Builder: Your Buddy in Test-Driven Development (TDD)
Favicon
AutoEntityGenerator – my first visual studio extension.
Favicon
C# | Visual Studio Extensions for C# Developers
Favicon
SonarQube | Working with SonarLint and SonarQube in Visual Studio
Favicon
Migrating a project from Visual studio to Rider
Favicon
How to embed your git bash into Visual Studio
Favicon
.NET Monthly Roundup - May 2024 - .NET at Build, .NET Aspire GA, and more!
Favicon
Quick Tipp: Dev Tunnels with Visual Studio
Favicon
EasyTdd 0.4.0 Release: Introducing the Incremental Builder
Favicon
Troubleshooting β€œThe File Does Not Have an App Associated with It” Error in Visual Studio
Favicon
Help Needed: "Object reference not set to an instance of an object" Error in Visual Studio
Favicon
Debugging with Precision: Conditional Breakpoints
Favicon
Simple Way To CreateYour NuGet Package: Step-by-Step Guide
Favicon
Easy Self-Hosted Git Installation on Ubuntu Server
Favicon
Effortless Remote Debugging with Dev Tunnel in Visual Studio 2022
Favicon
How to Use GitHub Copilot in Visual Studio 2024
Favicon
How to Enable Windows 11 Developer Mode?
Favicon
Visual Studio Live Share: Complete Guide 2024
Favicon
Gitlens Tutorial - Visual Studio Extension 2024
Favicon
Top 8 Best Visual Studio Extensions in 2024

Featured ones: