Is there an issue with set_dof_state_tensor_indexed when using mutiple actors in a single environment?

I have the following code.

def _reset_dofs(self, env_ids):
    self.dof_pos[env_ids] = self.default_dof_pos * torch_rand_float(0.9, 1.1,
                                                                    (len(env_ids), self.num_dof),
                                                                    device=self.device)
    self.dof_vel[env_ids] = 0.
    env_ids_int32 = env_ids.clone().to(dtype=torch.int32)
    self.gym.set_dof_state_tensor_indexed(self.sim,
                                          gymtorch.unwrap_tensor(self.dof_state),
                                          gymtorch.unwrap_tensor(env_ids_int32),
                                          len(env_ids_int32))


def reset_idx(self, env_ids):
    self._reset_dofs(env_ids)
    print(self.robot_root_states)
    self._reset_root_states(env_ids)

I can’t access the robot_root_states anymore after calling the reset_dofs(). I get the following error.

RuntimeError: CUDA error: an illegal memory access was encountered
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

The problem seems to be with set_dof_state_tensor_indexed. Please help!

2 Likes

Hello, I encounter the same problem. Have you solved this yet? Thank you!

Yes. When there are multiple actors in the environment and you want to change the dof states of only a particular actor in each environment, the index number of the actor has to be mentioned in the env_ids. For example, if I have a legged robot and two obstacles in each environment and I want to set the dof state of each legged robot, then the code has to be modified as follows.

def _reset_dofs(self,  env_ids):
    self.dof_pos[env_ids] = self.default_dof_pos * torch_rand_float(0.9, 1.1,
                                                                    (len(env_ids), self.num_dof),
                                                                    device=self.device)
    self.dof_vel[env_ids] = 0.
    env_ids_int32 = 3 * env_ids.clone().to(dtype=torch.int32)
    self.gym.set_dof_state_tensor_indexed(self.sim,
                                          gymtorch.unwrap_tensor(self.dof_state),
                                          gymtorch.unwrap_tensor(env_ids_int32),
                                          len(env_ids_int32))

env_ids is multiplied by 3, so that it’ll only set the dof state of legged robot in each environment assuming that the robot is the first actor in each env, i.e. the index number of all the robots will be 0, 3, 6…etc. The problem with the earlier version of the code is it was trying to set the dof state of the obstacles as well because of the not changed env_ids and that was causing the problem.

Thx a lot! It worked.