Main Content

Accelerate Linear Model Fitting on GPU

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 readtable function. Remove table rows corresponding to canceled flights, and convert UniqueCarrier to a categorical variable using the categorical function.

A = readtable("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® Xeon® W-2133 CPU @ 3.60 GHz.

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 fitlm function to fit a linear regression model to the variables in tblCPU. Measure the time required to run the anonymous function by using the timeit function.

cpufit = @() fitlm(tblCPU,CategoricalVars=4);
tcpu = timeit(cpufit)
tcpu = 0.3241

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 RTX A5000'
                Index: 1 (of 2)
    ComputeCapability: '8.6'
          DriverModel: 'TCC'
          TotalMemory: 25544294400 (25.54 GB)
      AvailableMemory: 25120866304 (25.12 GB)
      DeviceAvailable: true
       DeviceSelected: true

  Show all properties.

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 fitlm function, it automatically runs on the GPU. Measure the time required to fit the regression model on the GPU by using the gputimeit (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.1006

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.2904      33.3585     -5.3447    9.0739e-08
    Year                     0.09144843       0.0166      5.5089    3.6173e-08
    Month                   -0.07640735    0.0255798      -2.987     0.0028176
    DayofMonth               0.03731819    0.0100045      3.7301    0.00019147
    UniqueCarrier_AA           2.474112      1.38942      1.7807      0.074968
    UniqueCarrier_AQ          -4.028572      2.81772     -1.4297        0.1528
    UniqueCarrier_AS           3.478428      1.47986      2.3505      0.018749
    UniqueCarrier_B6           6.685668      1.73686      3.8493    0.00011853
    UniqueCarrier_CO           2.622961      1.40944       1.861      0.062748
    UniqueCarrier_DH           2.514483      1.79715      1.3991       0.16177
    UniqueCarrier_DL           3.071577      1.38827      2.2125      0.026932
    UniqueCarrier_EA           4.619557      1.73326      2.6652     0.0076942
    UniqueCarrier_EV           4.831761      1.55067      3.1159     0.0018342
    UniqueCarrier_F9           3.199074      2.15633      1.4836       0.13792
    UniqueCarrier_FL           4.286813      1.60874      2.6647     0.0077066
    UniqueCarrier_HA           -6.75125      2.29837     -2.9374     0.0033104
    UniqueCarrier_HP             3.2339      1.46058      2.2141      0.026822
    UniqueCarrier_ML (1)       -3.77572      3.92876    -0.96105       0.33653
    UniqueCarrier_MQ           3.739103      1.44475      2.5881      0.009653
    UniqueCarrier_NW          0.9346265       1.3994     0.66788       0.50421
    UniqueCarrier_OH           2.552324      1.58129      1.6141       0.10651
    UniqueCarrier_OO          0.6509585      1.46651     0.44388       0.65713
    UniqueCarrier_PA (1)       1.653223      2.21609     0.74601       0.45566
    UniqueCarrier_PI           7.609183      1.73962      4.3741    1.2206e-05
    UniqueCarrier_PS           1.854016      3.65042     0.50789       0.61153
    UniqueCarrier_TW           3.235745      1.46218       2.213      0.026902
    UniqueCarrier_TZ          -3.190202      2.48632     -1.2831       0.19946
    UniqueCarrier_UA           3.933074       1.3927      2.8241     0.0047426
    UniqueCarrier_US            2.40755      1.39284      1.7285      0.083898
    UniqueCarrier_WN          0.7392784      1.38333     0.53442       0.59305
    UniqueCarrier_XE           3.605406      1.49916       2.405      0.016176
    UniqueCarrier_YV           7.036055      1.72275      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 anova. 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