Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parallel loop execution?

    I have the following loop in Mata


    Code:
    mata:
    result = 0
    
    for (i = 1; i <= 100; i++) {
       result_i = // Operations using the i-th observation
       result = result + result_i
    }
    end
    This should be executable in parallel. Each thread takes, say, 50 observations and returns the sum, and at the end I collect each result. Something like

    Code:
    mata:
    result_thread1 = result_thread2 = 0
    
    // Thread 1
    waitfor_thread1 = 1
    for (i = 1; i <= 50; i++) {
        result_i = // Operations using the i-th observation
        result_thread1 = result + result_i
    }
    waitfor_thread1 = 0
    
    // Thread 2
    waitfor_thread2 = 1
    for (i = 51; i <= 100; i++) {
       result_i = // Operations using the i-th observation
       result_thread2 = result + result_i
    }
    waitfor_thread2 = 0
    
    // Collect
    while (waitfor_thread1 & waitfor_thread2) {}
    result = result_thread1 + result_thread2
    end
    But have the three code cunks execute in parallel. Is such a thing possible? Workarounds and hacks welcome.

    Thanks!
Working...
X