classner / pymp

Easy, OpenMP style multiprocessing for Python on Unix.
MIT License
278 stars 29 forks source link

Return strings instead of np.float64? #23

Closed laytonjbgmail closed 3 years ago

laytonjbgmail commented 3 years ago

Hello,

I'm still continuing with my project and I have a need to return a string that is not numeric (it's a file name) from each function rather than np.float64. My Python skills are not up to modifying shared.py to see if it's possible. Is this possible?

Thanks!

Jeff

classner commented 3 years ago

Hi @laytonjbgmail !

This is definitely possible. One of the simplest ways is to use a list instead of the array you used in the last example (the list can contain arbitrary Python objects). For example:

#!/usr/bin/env python3

def func1(data):
    print("   in func1")
    return "value1"

def func2(data):
    print("   in func2")
    return "value2"

def func3(data):
    print("   in func3")
    return "value3"

if __name__ == "__main__":
    import pymp

    # Initializing a three-element list instead
    # of an array. Because we know how long the list
    # will be we can do this in advance and then don't
    # have to use locks for accessing elements.
    results = pymp.shared.list([None] * 3)
    with pymp.Parallel(3) as p:
        for sec_idx in p.xrange(3):
            if sec_idx == 0:
                p.print("Section 1")
                data1 = [1, 2, 3, 4]
                results[0] = func1(data1)
            elif sec_idx == 1:
                p.print("Section 2")
                data2 = [5, 6, 7, 8]
                results[1] = func2(data2)
            elif sec_idx == 2:
                p.print("Section 3")
                data3 = [9, 10, 11, 12, 12, 13, 14, 15]
                results[2] = func3(data3)

    print(" ")
    print("total = ", results[0])
    print("median = ", results[1])
    print("avg = ", results[2])