Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset OpenMP thread number if num_threads <= 0 #4704

Merged
merged 8 commits into from
Oct 28, 2021

Conversation

hzy46
Copy link
Contributor

@hzy46 hzy46 commented Oct 22, 2021

fix
fix

fix

fix

fix

fix

fix

fix

fix

fix
@hzy46 hzy46 changed the title Reset OpenMP thread number if num_threads < 0 Reset OpenMP thread number if num_threads <= 0 Oct 22, 2021
@hzy46
Copy link
Contributor Author

hzy46 commented Oct 26, 2021

Use omp_get_num_threads + pragma omp parallel with a global variable will cause infinite waiting in Windows. I don't know the reason. Use omp_get_max_threads instead.

@hzy46
Copy link
Contributor Author

hzy46 commented Oct 28, 2021

The problem of infinite waiting in Windows is caused by ctypes.cdll.LoadLibrary in Python. To reproduce it, one can build the following C++ code to a DLL file in Windows.

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

int OMP_NUM_THREADS() {
    int ret = 1;
#pragma omp parallel
#pragma omp master
    { ret = omp_get_num_threads(); }
    return ret;
}

int default_omp_num_threads = OMP_NUM_THREADS();

Then use the following python code to load it:

import ctypes
dll_file = '''<path to the dll>'''
ctypes.cdll.LoadLibrary(dll_file)

Then the code will be blocked forever. I have changed the implementation to use a static variable in function to resolve the problem caused by global variable.

Copy link
Collaborator

@shiyu1994 shiyu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes LGTM. Thanks!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants