How to select gpu using FFMPEG?

AVDictionary* options = nullptr;
av_dict_set(&options, “hwaccel”, “cuvid”, 0);
av_dict_set_int(&options, “hwaccel_device”, 2, 0);

Even though I specify hwaccel device to be 2, it still runs on GPU 0. How can I fix this?

There is -init_hw_device cuda:0,primary_ctx=1.