main content

accelerate linear model fitting on gpu -凯发k8网页登录

this example shows how you can accelerate regression model fitting by running functions on a graphical processing unit (gpu). the example compares the time required to fit a model on a central processing unit (cpu) with the time required to fit the same model on a gpu. using a gpu requires parallel computing toolbox™ and a supported gpu device. for information about supported devices, see gpu computing requirements (parallel computing toolbox).

create a table of airline sample data from the file airlinesmall.csv by using the function. remove table rows corresponding to cancelled flights, and convert uniquecarrier to a categorical variable using the function.

a = readtable("\toolbox\matlab\demos\airlinesmall.csv");
a = a(a.cancelled~=1,:);
a.uniquecarrier = categorical(a.uniquecarrier)
a=121171×29 table
    year    month    dayofmonth    dayofweek    deptime    crsdeptime    arrtime    crsarrtime    uniquecarrier    flightnum    tailnum    actualelapsedtime    crselapsedtime    airtime    arrdelay    depdelay    origin      dest      distance    taxiin    taxiout    cancelled    cancellationcode    diverted    carrierdelay    weatherdelay    nasdelay    securitydelay    lateaircraftdelay
    ____    _____    __________    _________    _______    __________    _______    __________    _____________    _________    _______    _________________    ______________    _______    ________    ________    _______    _______    ________    ______    _______    _________    ________________    ________    ____________    ____________    ________    _____________    _________________
    1987     10          21            3          642          630         735          727            ps            1503       {'na'}             53                 57          {'na'}         8          12       {'lax'}    {'sjc'}       308      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          26            1         1021         1020        1124         1116            ps            1550       {'na'}             63                 56          {'na'}         8           1       {'sjc'}    {'bur'}       296      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          23            5         2055         2035        2218         2157            ps            1589       {'na'}             83                 82          {'na'}        21          20       {'san'}    {'smf'}       480      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          23            5         1332         1320        1431         1418            ps            1655       {'na'}             59                 58          {'na'}        13          12       {'bur'}    {'sjc'}       296      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          22            4          629          630         746          742            ps            1702       {'na'}             77                 72          {'na'}         4          -1       {'smf'}    {'lax'}       373      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          28            3         1446         1343        1547         1448            ps            1729       {'na'}             61                 65          {'na'}        59          63       {'lax'}    {'sjc'}       308      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10           8            4          928          930        1052         1049            ps            1763       {'na'}             84                 79          {'na'}         3          -2       {'san'}    {'sfo'}       447      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          10            6          859          900        1134         1123            ps            1800       {'na'}            155                143          {'na'}        11          -1       {'sea'}    {'lax'}       954      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          20            2         1833         1830        1929         1926            ps            1831       {'na'}             56                 56          {'na'}         3           3       {'lax'}    {'sjc'}       308      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          15            4         1041         1040        1157         1155            ps            1864       {'na'}             76                 75          {'na'}         2           1       {'sfo'}    {'las'}       414      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          15            4         1608         1553        1656         1640            ps            1907       {'na'}             48                 47          {'na'}        16          15       {'lax'}    {'fat'}       209      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          21            3          949          940        1055         1052            ps            1939       {'na'}             66                 72          {'na'}         3           9       {'lgb'}    {'sfo'}       354      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          22            4         1902         1847        2030         1951            ps            1973       {'na'}             88                 64          {'na'}        39          15       {'lax'}    {'oak'}       337      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          16            5         1910         1838        2052         1955            tw              19       {'na'}            162                137          {'na'}        57          32       {'stl'}    {'den'}       770      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10           2            5         1130         1133        1237         1237            tw              59       {'na'}            187                184          {'na'}         0          -3       {'stl'}    {'phx'}      1262      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
    1987     10          30            5         1400         1400        1920         1934            tw             102       {'na'}            200                214          {'na'}       -14           0       {'sna'}    {'stl'}      1570      {'na'}    {'na'}         0             {'na'}            0           {'na'}          {'na'}        {'na'}        {'na'}             {'na'}      
      ⋮

the table a contains data for 12,1171 flights. the table variables year, month, and dayofmonth contain data for the year, month, and day that each flight departed, respectively. arrdelay contains the delay in minutes between each flight's scheduled and actual arrival time. uniquecarrier contains data for the airline that operated each flight.

measure time to fit linear model on cpu

measure the time required to fit a linear regression model on a cpu to the predictor variables year, month, dayofmonth, and uniquecarrier, and the response variable arrdelay. this example uses an intel(r) xeon(r) cpu e5-2623 v4 @ 2.60ghz.

create a second table from the table a variables year, month, dayofmonth, uniquecarrier, and arrdelay.

tblcpu = table(a.year,a.month,a.dayofmonth,a.uniquecarrier,a.arrdelay,variablenames=["year" "month" "dayofmonth" "uniquecarrier" "arrdelay"])
tblcpu=121171×5 table
    year    month    dayofmonth    uniquecarrier    arrdelay
    ____    _____    __________    _____________    ________
    1987     10          21             ps              8   
    1987     10          26             ps              8   
    1987     10          23             ps             21   
    1987     10          23             ps             13   
    1987     10          22             ps              4   
    1987     10          28             ps             59   
    1987     10           8             ps              3   
    1987     10          10             ps             11   
    1987     10          20             ps              3   
    1987     10          15             ps              2   
    1987     10          15             ps             16   
    1987     10          21             ps              3   
    1987     10          22             ps             39   
    1987     10          16             tw             57   
    1987     10           2             tw              0   
    1987     10          30             tw            -14   
      ⋮

create an anonymous function that uses the function to fit a linear regression model to the variables in tblcpu. measure the time required to run the anonymous function by using the function.

cpufit = @() fitlm(tblcpu,categoricalvars=4);
tcpu = timeit(cpufit)
tcpu = 0.3886

tcpu contains the time required to fit the linear regression model on the cpu.

measure time to fit linear model on gpu

verify that a gpu device is available by using the gpudevice (parallel computing toolbox) function.

gpudevice
ans = 
  cudadevice with properties:
                      name: 'nvidia geforce rtx 2080 super'
                     index: 1
         computecapability: '7.5'
            supportsdouble: 1
     graphicsdriverversion: '512.15'
               drivermodel: 'wddm'
            toolkitversion: 11.2000
        maxthreadsperblock: 1024
          maxshmemperblock: 49152 (49.15 kb)
        maxthreadblocksize: [1024 1024 64]
               maxgridsize: [2.1475e 09 65535 65535]
                 simdwidth: 32
               totalmemory: 8589606912 (8.59 gb)
           availablememory: 6979084692 (6.98 gb)
               cachepolicy: 'balanced'
       multiprocessorcount: 48
              clockratekhz: 1815000
               computemode: 'default'
      gpuoverlapstransfers: 1
    kernelexecutiontimeout: 1
          canmaphostmemory: 1
           devicesupported: 1
           deviceavailable: 1
            deviceselected: 1

the output shows that this example uses an nvidia geforce rtx 2080 super gpu.

create a third table from the table tblcpu variables. copy the table's numeric and logical variables to gpu memory by using the gpuarray (parallel computing toolbox) function.

tblgpu = tblcpu;
for ii = 1:width(tblcpu)
    if isnumeric(tblcpu.(ii)) || islogical(tblcpu.(ii))
        tblgpu.(ii) = gpuarray(tblcpu.(ii));
    end
end
tblgpu
tblgpu=121171×5 table
    year    month    dayofmonth    uniquecarrier    arrdelay
    ____    _____    __________    _____________    ________
    1987     10          21             ps              8   
    1987     10          26             ps              8   
    1987     10          23             ps             21   
    1987     10          23             ps             13   
    1987     10          22             ps              4   
    1987     10          28             ps             59   
    1987     10           8             ps              3   
    1987     10          10             ps             11   
    1987     10          20             ps              3   
    1987     10          15             ps              2   
    1987     10          15             ps             16   
    1987     10          21             ps              3   
    1987     10          22             ps             39   
    1987     10          16             tw             57   
    1987     10           2             tw              0   
    1987     10          30             tw            -14   
      ⋮

the tblgpu table contains gpuarray objects, each representing an array stored in gpu memory.

when you pass gpuarray inputs to the function, it automatically runs on the gpu. measure the time required to fit the regression model on the gpu by using the (parallel computing toolbox) function. this function is preferable to timeit, because it ensures that all operations on the gpu are complete before it records the time. the function also compensates for the overhead of working on a gpu.

gpufit = @() fitlm(tblgpu,categoricalvars=4);
tgpu = gputimeit(gpufit)
tgpu = 0.1167

tgpu contains the time required to fit the linear regression model on the gpu, which is over three times faster than on the cpu.

determine statistical significance

to investigate the statistical significance of the linear regression model terms, fit the regression model on the gpu.

mdl = fitlm(tblgpu,categoricalvars=4)
mdl = 
linear regression model:
    arrdelay ~ 1   year   month   dayofmonth   uniquecarrier
estimated coefficients:
                            estimate        se        tstat        pvalue  
                            _________    ________    ________    __________
    (intercept)               -178.29      33.358     -5.3447    9.0739e-08
    year                     0.091448      0.0166      5.5089    3.6173e-08
    month                   -0.076407     0.02558      -2.987     0.0028176
    dayofmonth               0.037318    0.010005      3.7301    0.00019147
    uniquecarrier_aa           2.4741      1.3894      1.7807      0.074968
    uniquecarrier_aq          -4.0286      2.8177     -1.4297        0.1528
    uniquecarrier_as           3.4784      1.4799      2.3505      0.018749
    uniquecarrier_b6           6.6857      1.7369      3.8493    0.00011853
    uniquecarrier_co            2.623      1.4094       1.861      0.062748
    uniquecarrier_dh           2.5145      1.7972      1.3991       0.16177
    uniquecarrier_dl           3.0716      1.3883      2.2125      0.026932
    uniquecarrier_ea           4.6196      1.7333      2.6652     0.0076942
    uniquecarrier_ev           4.8318      1.5507      3.1159     0.0018342
    uniquecarrier_f9           3.1991      2.1563      1.4836       0.13792
    uniquecarrier_fl           4.2868      1.6087      2.6647     0.0077066
    uniquecarrier_ha          -6.7512      2.2984     -2.9374     0.0033104
    uniquecarrier_hp           3.2339      1.4606      2.2141      0.026822
    uniquecarrier_ml (1)      -3.7757      3.9288    -0.96105       0.33653
    uniquecarrier_mq           3.7391      1.4448      2.5881      0.009653
    uniquecarrier_nw          0.93463      1.3994     0.66788       0.50421
    uniquecarrier_oh           2.5523      1.5813      1.6141       0.10651
    uniquecarrier_oo          0.65096      1.4665     0.44388       0.65713
    uniquecarrier_pa (1)       1.6532      2.2161     0.74601       0.45566
    uniquecarrier_pi           7.6092      1.7396      4.3741    1.2206e-05
    uniquecarrier_ps            1.854      3.6504     0.50789       0.61153
    uniquecarrier_tw           3.2357      1.4622       2.213      0.026902
    uniquecarrier_tz          -3.1902      2.4863     -1.2831       0.19946
    uniquecarrier_ua           3.9331      1.3927      2.8241     0.0047426
    uniquecarrier_us           2.4076      1.3928      1.7285      0.083898
    uniquecarrier_wn          0.73928      1.3833     0.53442       0.59305
    uniquecarrier_xe           3.6054      1.4992       2.405      0.016176
    uniquecarrier_yv           7.0361      1.7228      4.0842     4.426e-05
number of observations: 120866, error degrees of freedom: 120834
root mean squared error: 30.5
r-squared: 0.00259,  adjusted r-squared: 0.00233
f-statistic vs. constant model: 10.1, p-value = 2.38e-48

mdl contains the formula for the linear regression model and statistics about the estimated model coefficients. the table output contains a row for each continuous term and for each value in uniquecarrier. you can determine if a term or value has a statistically significant effect on the arrival delay by comparing its p-value to the significance level of 0.05.

to determine whether uniquecarrier contains a value that has a statistically significant effect on the arrival delay, perform an anova with a 95% confidence level by using the function . when you pass gpuarray inputs to the anova function, it automatically runs on the gpu.

aov = anova(mdl)
aov=5×5 table
                       sumsq           df        meansq      f         pvalue  
                     __________    __________    ______    ______    __________
    year                  28309             1     28309    30.348    3.6173e-08
    month                8322.7             1    8322.7    8.9223     0.0028176
    dayofmonth            12979             1     12979    13.914    0.00019147
    uniquecarrier    2.4305e 05            28    8680.5    9.3059    1.6331e-39
    error            1.1271e 08    1.2083e 05    932.79                        

aov contains the results of the anova. the p-value in the row corresponding to uniquecarrier is smaller than the significance level of 0.05, indicating that at least one value in uniquecarrier has a statistically significant effect on the arrival delay.

see also

(parallel computing toolbox) | (parallel computing toolbox) | | |

related topics

网站地图