qq_36559915
qq_36559915
2016-12-10 08:33
采纳率: 0%
浏览 1.4k

如何用python 里面的numba库利用cuda编写代码(python2.7) 例如下面的代码。

 import math

def ReadMatchTime():
    path='E:\TS.txt'
    MatchTime=open(path,'r')
    time=[]
    for i in MatchTime:
        time.append(i.split())
    print(time[0][0])
    return time

global c,Pi
c= 299792.458
Pi=3.1415926535898

def rad(x):
    return (x*Pi*1.0 / 180);



def distance(LatA,LonA,LatB,LonB):
    LatA = rad(LatA);
    LatB = rad(LatB);
    LonA = rad(LonA);
    LonB = rad(LonB);
    R = 6371.004;
    acos=math.acos
    cos=math.cos
    sin=math.sin
    distance = R*acos(cos(LatA)*cos(LatB)*cos(LonB - LonA) + sin(LatA)*sin(LatB));
    return distance;

def Location():
    loc=[]
    lat=[]
    lon=[]
    Loc=[]
    LOC=[]
    for i in range(0,1000):
        lat=((lat1-lat2)/1000)+lat1
        for j in range(0,1000):
            lon=((lon1-lon2)/1000)+lon1
            loc=[lat,lon]
            Loc.append(loc)
        LOC.append(Loc)

def main():
    Time=ReadMatchTime()
    DIF=[1.0000000,1.0000000,1.0000000]

    DIF[0]=(c*abs(float(Time[0][0][12:20])-float(Time[0][1][12:20])))
    DIF[1]=(c*abs(float(Time[0][1][12:20])-float(Time[0][2][12:20])))
    DIF[2]=(c*abs(float(Time[0][2][12:20])-float(Time[0][0][12:20])))
    loc=[]
    lat=[]
    lon=[]
    Loc=[]
    LOC=[]
    lat1=30.111
    lat2=33.325
    lon1=100.453
    lon2=103.215

    stations=[[32.789,101.345],[31.729,101.345],[30.789,105.345]]
    #print(stations)
    for i in range(0,1000):
        lat=(((lat2-lat1)/1000)*i)+lat1
        for j in range(0,1000):
            lon=(((lon2-lon1)/1000)*j)+lon1
            loc=[lat,lon] #每一个点
            for k in range(0,3):
                Dis=[0,0,0]
                Dis_temp=[1.0000000,1.0000000,1.0000000]
                Dis[k]=distance(stations[k][0],stations[k][1],loc[0],loc[1])
                Dis_temp[k]=(Dis[k]-DIF[k])*(Dis[k]-DIF[k])
            sq=Dis_temp[0]+Dis_temp[1]+Dis_temp[2]
            #加上每一个点的埃克塞的平方
            loc.append(sq)
            Loc.append(loc)#每一个加点
            #print(Loc)
        LOC.append(Loc)#每加一行点
    #print(LOC[10][10])

    #冒泡找最小的sq 并返回经纬度
    final=LOC[0][0][2]
    I=0
    J=0
    for i in range(0,1000):
        for j in range(1,1000):
            if(LOC[i][j][2]<final):
                final=LOC[i][j][2]
                I=i
                J=j
    print(LOC[I][J])



if __name__ == "__main__":


    main()

这段代码想要表达的就是将经纬度分成网格(1000×1000)格,每个格能返回一个数值,
,请大神指点,如果用cuda因为对于速度有很高的要求。跪谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • qq_36559915
    qq_36559915 2016-12-10 08:56

    ```python #这里可以指定高亮语言类型#
    import math

    def ReadMatchTime():
    path='E:\TS.txt'
    MatchTime=open(path,'r')
    time=[]
    for i in MatchTime:
        time.append(i.split())
    print(time[0][0])
    return time
    

    global c,Pi
    c= 299792.458
    Pi=3.1415926535898

    def rad(x):
    return (x*Pi*1.0 / 180);

    def distance(LatA,LonA,LatB,LonB):
    LatA = rad(LatA);
    LatB = rad(LatB);
    LonA = rad(LonA);
    LonB = rad(LonB);
    R = 6371.004;
    acos=math.acos
    cos=math.cos
    sin=math.sin
    distance = R*acos(cos(LatA)*cos(LatB)*cos(LonB - LonA) + sin(LatA)*sin(LatB));
    return distance;

    def Location():
    loc=[]
    lat=[]
    lon=[]
    Loc=[]
    LOC=[]
    for i in range(0,1000):
    lat=((lat1-lat2)/1000)+lat1
    for j in range(0,1000):
    lon=((lon1-lon2)/1000)+lon1
    loc=[lat,lon]
    Loc.append(loc)
    LOC.append(Loc)

    def main():
    Time=ReadMatchTime()
    DIF=[1.0000000,1.0000000,1.0000000]

    DIF[0]=(c*abs(float(Time[0][0][12:20])-float(Time[0][1][12:20])))
    DIF[1]=(c*abs(float(Time[0][1][12:20])-float(Time[0][2][12:20])))
    DIF[2]=(c*abs(float(Time[0][2][12:20])-float(Time[0][0][12:20])))
    loc=[]
    lat=[]
    lon=[]
    Loc=[]
    LOC=[]
    lat1=30.111
    lat2=33.325
    lon1=100.453
    lon2=103.215
    
    stations=[[32.789,101.345],[31.729,101.345],[30.789,105.345]]
    #print(stations)
    for i in range(0,1000):
        lat=(((lat2-lat1)/1000)*i)+lat1
        for j in range(0,1000):
            lon=(((lon2-lon1)/1000)*j)+lon1
            loc=[lat,lon] #每一个点
            for k in range(0,3):
                Dis=[0,0,0]
                Dis_temp=[1.0000000,1.0000000,1.0000000]
                Dis[k]=distance(stations[k][0],stations[k][1],loc[0],loc[1])
                Dis_temp[k]=(Dis[k]-DIF[k])*(Dis[k]-DIF[k])
            sq=Dis_temp[0]+Dis_temp[1]+Dis_temp[2]
            #加上每一个点的埃克塞的平方
            loc.append(sq)
            Loc.append(loc)#每一个加点
            #print(Loc)
        LOC.append(Loc)#每加一行点
    #print(LOC[10][10])
    
    #冒泡找最小的sq 并返回经纬度
    final=LOC[0][0][2]
    I=0
    J=0
    for i in range(0,1000):
        for j in range(1,1000):
            if(LOC[i][j][2]<final):
                final=LOC[i][j][2]
                I=i
                J=j
    print(LOC[I][J])
    

    if name == "__main__":

    main()
    
    点赞 评论

相关推荐