鱼C论坛

 找回密码
 立即注册
查看: 2115|回复: 2

如何借助GPU提升效率?

[复制链接]
发表于 2021-5-9 13:25:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
非常感谢你在百忙之中抽空为我解答。
以下代码作用是在EXCEL表ABCDE列中选三列随机抽取4行做比较,由于效率很低,如果想通过调用GPU的并行运算去提高效率,该如何改呢?
  1. import xlrd
  2. import multiprocessing as mp
  3. from functools import partial
  4. import csv
  5. import random as rd

  6. def read_file(file):
  7.     data=xlrd.open_workbook(file)
  8.     names=data.sheet_names()
  9.     res=[]
  10.     for i in names:
  11.         table=data.sheet_by_name(i)
  12.         rows=table.nrows
  13.         res.append([table.row_values(j) for j in range(rows)])
  14.     res=[[[j[0],j[1],j[2],tuple(j[3].split('A')[1:3]),j[4],j[3]] for j in i] for i in res]
  15.     return(res,names)

  16. def write_file(file,res,names,maxlength):
  17.     head=[k for j in [[i]+['']*4 for i in names] for k in j]
  18.     f=open(file,'w',newline='')
  19.     csvw=csv.writer(f)
  20.     csvw.writerow(head)
  21.     for i in range(maxlength):
  22.         csvw.writerow([k for j in [data[i][:3]+data[i][:-3:-1] for data in res] for k in j])
  23.     f.close()

  24. def check(sample,data2,index,around):
  25.     res=[]
  26.     for i in sample:
  27.         if around!=-1:
  28.             temp=[j  for j in data2 if i[index[0]]==j[index[0]] and i[index[1]]==j[index[1]] and abs(i[index[2]]-j[index[2]])<=around]
  29.         else:
  30.             temp=[j  for j in data2 if i[index[0]]==j[index[0]] and i[index[1]]==j[index[1]] and i[index[2]]==j[index[2]]]
  31.         if len(temp):
  32.             res.append(temp[0])
  33.         else:
  34.             break
  35.     else:
  36.         return(res)
  37.     return([])
  38.                
  39. def cal(label,data,index,num,around):
  40.     if len(data[label])>=num:
  41.         sample=rd.sample(data[label],num)
  42.     else:
  43.         sample=data[label]
  44.     temp=[check(sample,i,index,around) for i in data[:label]+data[label+1:]]
  45.     if [j for i in temp for j in i]:
  46.         return([sample]+temp)
  47.     else:
  48.         return([])

  49. if __name__=='__main__':
  50.     alphatdict=dict([[chr(i),i-65] for i in range(65,91)])
  51.     file = "test.xlsx"
  52.     index=sorted([alphatdict[i] for i in input('需要比对的三列:').upper()])
  53.     if 4 in index:
  54.         around=0
  55.     else:
  56.         around=-1
  57.     num=4
  58.     filenum=10000
  59.     label=0
  60.     data,names=read_file(file)
  61.     ff=partial(cal,data=data,index=index,num=num,around=around)
  62.     while label<filenum:
  63.         pool=mp.Pool(mp.cpu_count())
  64.         res=pool.map(ff,range(len(data)))
  65.         pool.close()
  66.         pool.join()
  67.         res=[[k for j in i for k in j] for i in res]
  68.         maxlength=max([len(i) for i in res])
  69.         res=[i+[['']*6]*(maxlength-len(i)) for i in res]
  70.         if sum([len(i) for i in res]):
  71.             write_file('res'+str(label+1)+'.csv',res,names,maxlength)
  72.             label+=1
  73.         else:
  74.             pass
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-9 14:59:17 | 显示全部楼层
关键词 cuda,具体可以搜索: "cuda 编程" , “cuda cpp 编程", "cuda python 编程”
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-23 23:27:28 | 显示全部楼层
有哪位高人可以指点一下吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 13:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表