标题:【求助】如何借助GPU提升效率?
只看楼主
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
结帖率:87.5%
 问题点数:0 回复次数:1 
【求助】如何借助GPU提升效率?
非常感谢你在百忙之中抽空为我解答。
以下代码作用是在EXCEL表ABCDE列中选三列随机抽取4行做比较,由于效率很低,如果想通过调用GPU的并行运算去提高效率,该如何改呢?
程序代码:
import xlrd
import multiprocessing as mp
from functools import partial
import csv
import random as rd

def read_file(file):
    data=xlrd.open_workbook(file)
    names=data.sheet_names()
    res=[]
    for i in names:
        table=data.sheet_by_name(i)
        rows=table.nrows
        res.append([table.row_values(j) for j in range(rows)])
    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]
    return(res,names)

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

def check(sample,data2,index,around):
    res=[]
    for i in sample:
        if around!=-1:
            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]
        else:
            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]]]
        if len(temp):
            res.append(temp[0])
        else:
            break
    else:
        return(res)
    return([])
                
def cal(label,data,index,num,around):
    if len(data[label])>=num:
        sample=rd.sample(data[label],num)
    else:
        sample=data[label]
    temp=[check(sample,i,index,around) for i in data[:label]+data[label+1:]]
    if [j for i in temp for j in i]:
        return([sample]+temp)
    else:
        return([])

if __name__=='__main__':
    alphatdict=dict([[chr(i),i-65] for i in range(65,91)])
    file = "test.xlsx"
    index=sorted([alphatdict[i] for i in input('需要比对的三列:').upper()])
    if 4 in index:
        around=0
    else:
        around=-1
    num=4
    filenum=10000
    label=0
    data,names=read_file(file)
    ff=partial(cal,data=data,index=index,num=num,around=around)
    while label<filenum:
        pool=mp.Pool(mp.cpu_count())
        res=pool.map(ff,range(len(data)))
        pool.close()
        pool.join()
        res=[[k for j in i for k in j] for i in res]
        maxlength=max([len(i) for i in res])
        res=[i+[['']*6]*(maxlength-len(i)) for i in res]
        if sum([len(i) for i in res]):
            write_file('res'+str(label+1)+'.csv',res,names,maxlength)
            label+=1
        else:
            pass
搜索更多相关主题的帖子: data index res for label 
2021-05-09 13:24
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
得分:0 
有哪位高人可以指点一下吗?
2021-05-23 23:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505699-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.079392 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved