0

The Algorithm X in 30 lines https://www.cs.mcgill.ca/~aassaf9/python/algorithm_x.html

  if not X:
      yield list(solution)
  else:
      c = min(X, key=lambda c: len(X[c]))
      for r in list(X[c]):
          solution.append(r)
          cols = select(X, Y, r)
          for s in solve(X, Y, solution):
              yield s
          deselect(X, Y, r, cols)
          solution.pop()

def select(X, Y, r):
  cols = []
  for j in Y[r]:
      for i in X[j]:
          for k in Y[i]:
              if k != j:
                  X[k].remove(i)
      cols.append(X.pop(j))
  return cols

def deselect(X, Y, r, cols):
  for j in reversed(Y[r]):
      X[j] = cols.pop()
      for i in X[j]:
          for k in Y[i]:
              if k != j:
                  X[k].add(i)


In this code I want to parallelize the code fragment:

for r in list(X[c]):
            solution.append(r)
            cols = select(X, Y, r)
            for s in solve(X, Y, solution):
                yield s
            deselect(X, Y, r, cols)

I made a function:

def ParallelCode(X,Y,r,solution)
            print("Hello")
            solution.append(r)
            cols = select(X, Y, r)
            for s in solve(X, Y, solution):
                yield s
            deselect(X, Y, r, cols)
            solution.pop()
            solution.pop()

Then call it like this

if not X:
      yield list(solution)
  else:
      c = min(X, key=lambda c: len(X[c]))
      processes = [multiprocessing.Process(target=ParallelCode, args=(X, Y,r,solution)) for r in list(X[c])]

       for p in processes: 
            p.start()

       for p in processes:
           p.join()

When I ran the code, its not working. I print "Hello" inside ParallelCode, even "Hello" is not printed. Could you please help me what is wrong in this code.

1
  • 1
    I suppose there are two issues. Print may fail depending on where the stdout gets directed (try writing to a file and flushing). You are not actually running the process. Your function returns a generator and does nothing. Commented Dec 2, 2019 at 7:46

1 Answer 1

1

Your call to ParallelCode doesn't run the function. It rather returns a generator object. Either remove the yield, or wrap the function so it calls the generator and then returns.

If i understand correctly, solution parameter in your case acts as an accumulator. You can't return a value from multiprocessing. Instead use Queue and pass it as parameter, and let the new process write the result inside of it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I will let you know about the results. Let me try again using Queue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.