Logging in a parfor

3 views (last 30 days)
Ben
Ben on 2 Mar 2016
Edited: Adam on 2 Mar 2016
So this is multithreading 101. I have a crash somewhere, I assume. I started running 500 iterations and only about 40 out of 400 of them wrote a picture to file. I can't explain why so I am looking at debugging. In any other programming language, I would open up a log file with the id of the iteration (like log_1.log) and close the file at the end of the loop. How do I do this in matlab? I don't see how "diary" can be used since it seems to open up a global file, which is obviously a terrible idea. I don't want to use fprint statements since I am trying to collect everything in the command window on that specific thread. I might be making a large assumption about how parfor works though.
Can I assume that the parfor loop essentially is a fancy "fork" command? That is, I set up the variables and it hits a parfor and there is absolutely no communication between the different threads, essentially treating them as separate processes with separate heaps and stacks?
  1 Comment
Adam
Adam on 2 Mar 2016
Edited: Adam on 2 Mar 2016
In a simple parfor loop every thread should be independent of every other.
I'm not sure why you specifically don't want to use fprintf statements though. If you really want to write 500 log files this seems like the simplest way. I have previously used parfor loops writing each to their own file which worked well for what I wanted.
One of the easiest first ports of call for bugs in parfor loops though is to switch out the parfor for a regular for loop and see if you have the same problem. If you do then obviously you can ignore the parallel aspect and debug normally. If you don't get the problem then you can at least be sure it is related to the parfor specifically.

Sign in to comment.

Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!