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

The Library is not able to release the locks - ARM platform #23

Open
vishakhDuggal opened this issue Oct 28, 2024 · 3 comments
Open

The Library is not able to release the locks - ARM platform #23

vishakhDuggal opened this issue Oct 28, 2024 · 3 comments

Comments

@vishakhDuggal
Copy link

Hello,

I am building this library for ARM with GCC11.4 but found that if I launch my code as process using fork ( linux ) which call GAPP the code hangs and unable to proceed but if launched as boost::thread it works !!!!

if (auto* local_queue = local_worker_queue(); !local_queue)
{
remaining_tasks.wait();
}
else while (true)
{
while (!local_queue->empty()) { std::invoke(*local_queue->take()); }
if (remaining_tasks.try_wait()) break;
std::this_thread::yield();
}

The code gets stuck in While true loop in thread_pool.hpp. Please suggest !!! It works nicely in Intel

I even tried looking into architecture specific code : This also doesn't work

define GAPP_PAUSE() asm volatile("yield" ::: "memory") in utility.hpp

I am stuck for 2 weeks, any hit would be helpful

@KRM7
Copy link
Owner

KRM7 commented Oct 28, 2024

Hi,

  1. What version of the library are you using? Can you try the latest version from master to see if it works?
  2. If you are using the latest code from master and it still doesn't work, can you also try to run everything in the library on a single thread? You should be able to do this by calling gapp::execution_threads(1) before running things from the library (and before calling fork). You can also try doing something like setting the number of threads to 1 before forking, and setting it back to a higher value after the fork call.
  3. Can you provide more details about the issue? Please provide a complete example that can reproduce the issue, and a more complete explanation about what gets stuck, with stack traces.
  4. If you can, prefer using std::jthread and std::thread over fork. It's generally not a good idea to mix multiple threads and fork.

@vishakhDuggal
Copy link
Author

Hello, I solved the issue, there is a bug in the library and major code issue : Let me explain the issue first, I created my code which links to the GAPP library as dynamic linked library. and my code was launched using Fork(), unfortunately code was hanging as Semaphores were not getting released. After careful debugging found the issue

struct execution_context
{
GAPP_API inline static thread_pool global_thread_pool;
};

Thread_pool.cpp, this creates the instance of thread_pool when library is loaded ( when parent process is loaded ) but was getting used by child process where semaphore were locked this was changed to

/* struct execution_context
{
GAPP_API inline static thread_pool global_thread_pool;
};*/

template<typename F, typename Iter>
requires std::invocable<F, std::iter_reference_t<Iter>>
void parallel_for(Iter first, Iter last, F&& f)
{
  
     GAPP_API  static thread_pool global_thread_pool;
     global_thread_pool.execute_loop(first, last, 1, std::forward<F>(f));
   
}

Thus ensuring that the code only executes when object is created, if you find this useful please add to main branch.

  1. Major Issue, I have a requirement of creating multiple instances of GA in my code and run them in parallel, but current code structure would fail in this as parallel_for is not part of any class hence all instances will use the same call, which is also not thread same, putting the parallel_for also in class structure holding instance of thread_pool would solve the issue. Please let me know, I can support if required and you find the information useful.

@KRM7
Copy link
Owner

KRM7 commented Nov 14, 2024

Please read my first comment, especially points 2 and 4. You can't just fork a multithreaded process, there is no issue with the library in this respect.
Regarding your second point, you should be able to do what you want without any issues. There are no threading issues in parallel_for.

See also:
https://stackoverflow.com/questions/12868724/threads-and-fork-how-can-i-deal-with-that
https://stackoverflow.com/questions/1235516/fork-in-multi-threaded-program

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants