Posts

Showing posts with the label multithreading

ThreadPool and Pool for parallel processing

Image
Clash Royale CLAN TAG #URR8PPP ThreadPool and Pool for parallel processing Is there a way to use both ThreadPool and Pool in python to parallelise a loop by specifying the number of CPUs and cores you wish to use? For example I would have a loop execute as: from multiprocessing.dummy import Pool as ThreadPool from tqdm import tqdm import numpy as np def my_function(x): return x + 1 pool = ThreadPool(4) my_array = np.arange(0,1e6,1) results = list(tqdm(pool.imap(my_function, my_array),total=len(my_array))) For 4 cores (threads) but it I wanted to spread these out on multiple CPUs as well, is there a simple way to adapt the code? 1 Answer 1 You can just use it with no parameter and let the library decide: from multiprocessing.pool import ThreadPool ... pool = ThreadPool() But that will not share the process across multiple CPUs no? Only cores – user...

Thread.sleep another class

Image
Clash Royale CLAN TAG #URR8PPP Thread.sleep another class I have a class, which inserts values in a database (e.g. insertdb.java). But I only want them to be inserted every full hour, so after a little bit of researching and trying different approaches, I tried it with a Thread.sleep. How can I implement this code with the insertdb.java class (not a method)? Thanks in advance for your tips! import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class thread { public static void main(String args) { ScheduledExecutorService t = Executors.newSingleThreadScheduledExecutor(); t.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { Thread.sleep(3600000); } catch (Exception e) {} } }, 0, 1, TimeUnit.HOURS); } } 1 Answer 1 ...

Nested for loop, data dependency [ OpenMP ]

Image
Clash Royale CLAN TAG #URR8PPP Nested for loop, data dependency [ OpenMP ] I have a matrix solver (BiCCG) which is being used to solve set of algebraic equations arising from a 3 dimensional computational domain. I have tried to parallelise it using OpenMP but struggling with performance issues. On inspecting the code using Intel Advisor, it is evident that almost 80% of the solution time goes in the solver out of which there is one function which accounts for 50% of the solution time. Digging even deeper it is found that 5 loops out of 6 loops are performing terribly with no automatic vectorization since they suffer from data dependencies. What I do see is that there is a dependency (for eg in loop 3 ) because there i th iteration is using i-1 iteration's values. How to change the design of the parallelisation such that it can be the most efficient with this algorithm ( rather that changing the algorithm altogether). Whether specifying #pragma omp simd safelen(1) would help. #pra...

Java: multiple threads read the same InputStream at the same time

Image
Clash Royale CLAN TAG #URR8PPP Java: multiple threads read the same InputStream at the same time There is private InputStream inputStream; . Now, I would like to split the inputStream into 10 chunks, which return another 10 InputStream. 10 threads will read the 10 InputStream at the same time. How to do this? private InputStream inputStream; Does the following piece of codes work? BoundedInputStream stream = new BoundedInputStream(inputStream, chunkSize); This one? "(BoundedInputStream) will only supply bytes up to a certain length - if its position goes above that, it will stop." – user2864740 21 hours ago In general, each thread must have it's own InputStream (or provide the necessary synchronization over a shared object), to prevent race-condi...

Why does List.Add from multiple threads lead to varying results?

Image
Clash Royale CLAN TAG #URR8PPP Why does List<>.Add from multiple threads lead to varying results? I was wondering, if List<T> is thread-safe and read that several readers are no problem, but more than one writer may cause issues. So I wrote the following test to see what actually happens. List<T> [TestClass] public class ListConcurrency { [TestMethod] public void MultipleWritersTest() { var taskCnt = 10; var addCnt = 100; var list = new List<object>(); var tasks = new List<Task>(); for (int i = 0; i < taskCnt; i++) { var iq = i; tasks.Add(Task.Run(() => { Console.WriteLine("STARTING : " + iq); for (int j = 0; j < addCnt; j++) { try { list.Add(new object()); } catch (Exception e) ...

Why a segmentation fault occurs calling a function inside setjmp()?

Image
Clash Royale CLAN TAG #URR8PPP Why a segmentation fault occurs calling a function inside setjmp()? I do not understand why in the function middleFunc() , a segmentation fault is raisen when entry_point(arg) is invoked inside the if ( setjmp(middle) ) statement. middleFunc() entry_point(arg) if ( setjmp(middle) ) #include <stdio.h> #include <setjmp.h> jmp_buf start,middle,end; void finalFunc(void *v) { printf("hellon"); return ; } void middleFunc(void (*entry_point)(void *), void *arg) { //just debug : this does not cause segmentation fault entry_point(arg); if ( setjmp(middle) ){ //this casues the segmentation fault entry_point(arg); //once the entry point (finalFunc) is executed go to jmp_buffer end longjmp(end,1); } else { longjmp(start,1); } } int main(){ if (setjmp(end)){ //exit since finalFunc has been executed return ...

Multithread Pathfinding, Unit can't receive path array result

Image
Clash Royale CLAN TAG #URR8PPP Multithread Pathfinding, Unit can't receive path array result I have a pathfinder program using A* that i made following a tutorial. It works fine until i tried to make it multithread using async Task. The Thread works and the path is calculated, but when the path result is being passed to the Unit object, for some resaon only one or two Unit that receive the path array. where did i do wrong ? RequestManager : public class ThreadRequestManager : MonoBehaviour { Queue<PathResult> results = new Queue<PathResult>(); static ThreadRequestManager instance; ThreadPathfinding pathfinding; private void Awake() { instance = this; pathfinding = GetComponent<ThreadPathfinding>(); } private void Update() { if (results.Count > 0) { int itemsInQueue = results.Count; lock (results) { for(int i = 0; i < itemsInQueue; i++) { PathResult result = results.Dequeue(); ...

How DispatchQueue.asyncAfter behaves when application goes to background?

How DispatchQueue.asyncAfter behaves when application goes to background? I would like to know how DispatchQueue.asyncAfter(deadline:execute:) behaves in case of going to background. I tried to find more information in documentation, but it's nothing there. DispatchQueue.asyncAfter(deadline:execute:) Let's assume case in which I invoke: DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { ... } DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { ... } The most interesting part to me is to know when the timer counting down 60 seconds stops and resumes. Maybe I should use DispatchQueue.asyncAfter(wallDeadline:execute:) to be independent from background/foreground mode and timer freezing? DispatchQueue.asyncAfter(wallDeadline:execute:) Give a try and let us know :) – Prashant Tukadiya 37 mins ago ...

C MultiThread Value not printed

C MultiThread Value not printed First, Here is an entire code. 1. Read the file 2. Input file content into structure value 3. Begin Multithread $ cat sunje.conf { DSN:GOLDILOCKS UID:TEST PWD:test COMMIT INTERVAL:1 } $ cat sh.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <sys/time.h> //EXEC SQL INCLUDE SQLCA; typedef struct thread { char *dsn; char *uid; char *pwd; char mode; int num; int start; int end; int interval; }thread; pthread_mutex_t mutex; void *MultiThread(void* threadInfo); int main(int argc, char **argv) { char s[1024]; FILE *fp; char conn[4][1024]; int i = 0; int session = 0; int record = 0; char mode = NULL; //int interval; fp = fopen("sunje.conf", "r"); while(!feof(fp)) { fgets(s, 1024, fp); char *ptr = strchr(s, ':'); if (ptr != NULL) { strcpy(conn[i], ptr + ...