# New Parallel For Loops

The new parallel for loops provided by the new GAUSS keyword threadFor, in version 15 give you a compact method to add threading to your GAUSS program.

## Rolling Regression Example

Rolling regression is a natural candidate for parallelization. First, we load some data and prepare our variables for the rolling regression:
``````
filename = "ar1data.xlsx";
range = "a2:b501";
sheet = 1;

//Create x and y variable
x = packr(lag1(data[.,2]));
y = data[2:rows(data),2];

//Window for rolling regression
window = 50;``````

Now we are ready to perform the regression. Here is an implementation using a standard for loop:

``````//Pre-initialize vector to hold estimates
b = zeros(rows(x)-window,1);

//Estimate each window
for i(1,rows(x)-window,1);
b[i] = y[i:i+window]/x[i:i+window];
endfor;

//Combine and print out our results
print "average beta"; meanc(b);``````

The threads created by threadFor all share the same memory, so each thread can access any of the GAUSS global variables. There is no problem reading from the same global variable in multiple threads. With the threadFor loop in GAUSS you can even assign to the same global variable from multiple threads as long as you are not assigning to the same elements from more than one thread. Therefore, all we can parallelize the above loop, by simply change the for to threadFor and the endfor to threadEndFor like this:
``````//Pre-initialize vector to hold estimates
b = zeros(rows(x)-window,1);

//Estimate each window in parallel
b[i] = y[i:i+window]/x[i:i+window];

//Combine and print out our results
print "average beta"; meanc(b);``````

### Have a Specific Question?

Get a real answer from a real person

### Need Support?

Get help from our friendly experts.
{"ft_lang_label":"__label__en","ft_lang_prob":0.77146703,"math_prob":0.95827943,"size":1689,"snap":"2022-27-2022-33","text_gpt3_token_len":426,"char_repetition_ratio":0.12106825,"word_repetition_ratio":0.10699589,"special_character_ratio":0.24452339,"punctuation_ratio":0.15088758,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98598623,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T04:37:28Z\",\"WARC-Record-ID\":\"<urn:uuid:24c7cd8f-3cdc-4081-a999-5e127ff1df79>\",\"Content-Length\":\"132803\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3f663381-c816-49ce-92ea-a82d809a9713>\",\"WARC-Concurrent-To\":\"<urn:uuid:c0d2b8aa-8737-4044-a0a0-3868ee37d066>\",\"WARC-IP-Address\":\"104.21.89.175\",\"WARC-Target-URI\":\"https://www.aptech.com/releases/gauss15/new-parallel-for-loops/\",\"WARC-Payload-Digest\":\"sha1:XVIEUVD3JNHM6P5RIFMWDPXXBQELVJHC\",\"WARC-Block-Digest\":\"sha1:ISV6NMY3OHD5VRG45RFVCHEPLRPSNHLI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103620968.33_warc_CC-MAIN-20220629024217-20220629054217-00282.warc.gz\"}"}