os.environ["CUDA_VISIBLE_DEVICES"] = "0"无法调用GPU

成功安装了CUDA和cuDDN,但却无法调用GPU

输入:
from numba import cuda
print(cuda.gpus)
输出:
<Managed Device 0>
这里是正常的


输入:


import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from numba import cuda

def cpu_print():
    print("print by cpu.")

@cuda.jit
def gpu_print():
    # GPU核函数
    print("print by gpu.")

def main():
    gpu_print[1, 2]()
    cuda.synchronize()
    cpu_print()
main()

输出:
print by cpu.

到这里只有cpu能用,gpu却不显示

用任务管理器看GPU就没动过
试了下这个代码:

import tensorflow as tf
import timeit
 
with tf.device('/cpu:0'):
    cpu_a = tf.random.normal([10000, 1000])
    cpu_b = tf.random.normal([1000, 2000])
    print(cpu_a.device, cpu_b.device)
 
with tf.device('/gpu:0'):
    gpu_a = tf.random.normal([10000, 1000])
    gpu_b = tf.random.normal([1000, 2000])
    print(gpu_a.device, gpu_b.device)
 
def cpu_run():
    with tf.device('/cpu:0'):
        c = tf.matmul(cpu_a, cpu_b)
    return c
 
def gpu_run():
    with tf.device('/gpu:0'):
        c = tf.matmul(gpu_a, gpu_b)
    return c
 

cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)
 
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)

输出:
/device:CPU:0 /device:CPU:0
/device:GPU:0 /device:GPU:0
warmup: 0.007956899999044253 0.007983800000147312
run time: 0.010697099998651538 0.004664500000217231

我希望能找到上面第二个代码为什么GPU没反应的原因
验证os.environ["CUDA_VISIBLE_DEVICES"] = "0"可以调用我这块显卡

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。