2
votes

I've been recently dealing with a nested for loops in SBCL. I discovered, that operations, which can be reduced to map, can easily be made parallel using pmap (or any similar function). I decided to try and make my double for loop parallel in the following way:

Basic loops:

(loop for element in list_of_lists
    do (loop for element2 in list_of_lists2
        ...(random_function element element2)))

And this works fine, yet I was wondering if something like:

(defun supplementary_function (single_list list_collection)
    "This function iterates through list_collection and executes (random_function) on pairs obtained"
    (loop for element in list_collection
        do (random_function single_list element)))

(map 'nil (lambda (x) (supplementary_function x list_of_lists2)) list_of_lists1)

I would like to this for performance boost, as map in this case can easily be replaced with pmap in my case. So to clarify a bit more, the question is:

Can one replace the first loop with map, where in each map operation, second loop is executed within a special function of some sort using a single element from first loop and whole second loop. I see no conceptual mistake as to why this should not be possible, yet it throws me some memory errors (?)

Thank you very much!

1

1 Answers

2
votes

Does this help?

(loop for e1 in '(1 2 3 4 5)
      do (loop for e2 in '(a b c d e)
               do (print (list e1 e2))))

is

(mapc (lambda (e1)
        (mapc (lambda (e2)
                (print (list e1 e2)))
              '(a b c d e))) 
      '(1 2 3 4 5))

is

(defun f (e1 e2s)
  (mapc (lambda (e2)
          (print (list e1 e2)))
        e2s))

(mapc (lambda (e1)
        (f e1 '(a b c d e)))
      '(1 2 3 4 5))