In the code, self.x is set to [1., 1., 1., 1., 1.] and it is allocatted on GPU. While, it changes to [0., 0., 0., 0., 0.] when printing it in multiprocessing. And if the line of code 'self.x = self.x.to(device)' is deleted, the printing turns out to be correct. Why does this happen? And how to keep the value of data on GPU unchanged in multiprocessing?
import torch
import torch.multiprocessing as mp
class Set:
def __init__(self, device):
self.x = torch.ones(5)
self.x = self.x.to(device)
def solve(data):
print(data.x)
def func():
jobs = []
data = Set(torch.device('cuda:0'))
for i in range(3):
p = mp.Process(target=solve, args=(data,))
jobs.append(p)
p.start()
for j in jobs:
j.join()
if __name__ == '__main__':
func()