Parallelize Issue

This is my code:
alias NTHREADS = 4

fn xyz(A: UnsafePointer[Float32], B: UnsafePointer[Float32], nc:Int):
B.store(nc, A.load(nc))

fn main():
var A = UnsafePointer[Float32].alloc(4)
var B = UnsafePointer[Float32].alloc(4)

for i in range(4):
A[i] = i
B[i] = 1

print("Before:")
print("A:", A.load[width=4](0))
print("B:", B.load[width=4](0))

@parameter
fn parallel_xyz(thread_id: Int):
# print(thread_id)
xyz(A + (thread_id), B + (thread_id), thread_id)

total_items = 4
parallelize[parallel_xyz] (total_items, NTHREADS)

# print("After:")
# print("A:", A.load[width=4](0))
# print("B:", B.load[width=4](0))
alias NTHREADS = 4

fn xyz(A: UnsafePointer[Float32], B: UnsafePointer[Float32], nc:Int):
B.store(nc, A.load(nc))

fn main():
var A = UnsafePointer[Float32].alloc(4)
var B = UnsafePointer[Float32].alloc(4)

for i in range(4):
A[i] = i
B[i] = 1

print("Before:")
print("A:", A.load[width=4](0))
print("B:", B.load[width=4](0))

@parameter
fn parallel_xyz(thread_id: Int):
# print(thread_id)
xyz(A + (thread_id), B + (thread_id), thread_id)

total_items = 4
parallelize[parallel_xyz] (total_items, NTHREADS)

# print("After:")
# print("A:", A.load[width=4](0))
# print("B:", B.load[width=4](0))
I get the following output error:
Before:
A: [0.0, 1.0, 2.0, 3.0]
B: [1.0, 1.0, 1.0, 1.0]
[24010:24010:20241114,152857.053919:ERROR elf_dynamic_array_reader.h:64] tag not found
[24010:24010:20241114,152857.054009:ERROR elf_dynamic_array_reader.h:64] tag not found
[24010:24010:20241114,152857.054422:ERROR elf_dynamic_array_reader.h:64] tag not found
[24010:24010:20241114,152857.054484:ERROR elf_dynamic_array_reader.h:64] tag not found
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
mojo crashed!
Please file a bug report.
Before:
A: [0.0, 1.0, 2.0, 3.0]
B: [1.0, 1.0, 1.0, 1.0]
[24010:24010:20241114,152857.053919:ERROR elf_dynamic_array_reader.h:64] tag not found
[24010:24010:20241114,152857.054009:ERROR elf_dynamic_array_reader.h:64] tag not found
[24010:24010:20241114,152857.054422:ERROR elf_dynamic_array_reader.h:64] tag not found
[24010:24010:20241114,152857.054484:ERROR elf_dynamic_array_reader.h:64] tag not found
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
mojo crashed!
Please file a bug report.
But if I un-comment the last three lines of code, it works fine. Can anyone please help!
1 Reply
Martin Vuyk
Martin Vuyk3w ago
This seems like a problem we are still having with closures not extending the lifetime of variables. You'll have to manually discard the variables after the call to parallelize
parallelize[...](...)
_ = A^
_ = B^
parallelize[...](...)
_ = A^
_ = B^
And you're not freeing the pointer after using it, you'll have a memory leak if this is called by another function etc. with this example the memory gets freed once the program ends, but in a larger codebase it will lead to problems
Want results from more Discord servers?
Add your server