Skip to content

Instantly share code, notes, and snippets.

@felixjung
Created June 19, 2014 11:13
Show Gist options
  • Save felixjung/a723fc8ad71e7ffc5c8e to your computer and use it in GitHub Desktop.
Save felixjung/a723fc8ad71e7ffc5c8e to your computer and use it in GitHub Desktop.
Sample julia code
# Load library files
include("lib/parallel.jl")
# Set number of workers
setprocs(2)
# Load remaining source onto all parallel processes
require("lib/DataIO.jl")
require("lib/IntensityFunctions.jl")
# Set data folder variable
data_folder = "../../Data"
# Read in- and output of Duan model
tic()
firmdata = readallfirms("$data_folder/firm_data")
macrodata = readmacrocovariates(data_folder)
estimates = readestimates(data_folder)
time_io = toc();
###
# Resizing for testing
firmdata = firmdata[:, :, 1:4]
######
# Get all relevant sizes
tslength = size(macrodata, 2)
nfirms = size(firmdata, 3)
nhorizons = size(estimates, 2)
ncovariates = int(size(estimates, 1) / 2)
println("Running parallel computations.")
# Compute the macro part of intensities
macro_int_def_d = dzeros((nhorizons, tslength), workers(), [1, nworkers()])
macro_int_oe_d = dzeros((nhorizons, tslength), workers(), [1, nworkers()])
tic() # Start time taking
@parallel for w = workers()
# Prepare local access to distributed arrays
macro_int_def_dl = localpart(macro_int_def_d)
macro_int_oe_dl = localpart(macro_int_oe_d)
local_indices = myindexes(macro_int_def_d)
# Loop through the localpart of the DArrays and obtain corresponding indeces in non distributed arrays
# through local_indeces
for t = 1:length(local_indices[2])
for j = 1:length(local_indices[1])
macro_int_def_dl[j, t] = covar_to_intensity(macrodata[:, local_indices[2][t]], estimates[2:3, local_indices[1][j]])
macro_int_oe_dl[j, t] = covar_to_intensity(macrodata[:, local_indices[2][t]], estimates[13 + (2:3), local_indices[1][j]])
end
end
end
time_macro_par = toc();
# Compute the firm specific part of intensities
def_intensities_d = dzeros((nhorizons, tslength, nfirms), workers(), [1, 1, nworkers()])
oe_intensities_d = dzeros((nhorizons, tslength, nfirms), workers(), [1, 1, nworkers()])
tic()
@parallel for w = workers()
# Prepare local access to distributed arrays
def_intensities_dl = localpart(def_intensities_d)
oe_intensities_dl = localpart(oe_intensities_d)
local_indices = myindexes(def_intensities_d)
# Loop through the the local part of the DArrays and fill them
for i = 1:length(local_indices[3])
for t = unique(findn(firmdata[:, :, local_indices[3][i]])[2]) # It is OK to use the outside array, because indices are equal
for j = 1:length(local_indices[1])
def_intensities_dl[j, t, i] = covar_to_intensity(firmdata[:, t, local_indices[3][i]], estimates[4:end - 13, j])
oe_intensities_dl[j, t, i] = covar_to_intensity(firmdata[:, t, local_indices[3][i]], estimates[13 + 4:end, j])
end
end
end
end
time_firms_par = toc();
# Compute the intercept part of intensities
intercept_int_def = exp(estimates[1, :])
intercept_int_oe = exp(estimates[14, :])
# Add macro and intercept parts to firm specific intensities
tic()
@parallel for w = workers()
# Prepare local access to distributed arrays
def_intensities_dl = localpart(def_intensities_d)
oe_intensities_dl = localpart(oe_intensities_d)
macro_int_def = convert(Array, macro_int_def_d)
macro_int_oe = convert(Array, macro_int_oe_d)
local_indices = myindexes(def_intensities_d)
print(local_indices)
# Loop through
for i = 1:length(local_indices[3])
for t = unique(findn(firmdata[:, :, local_indices[3][i]])[2])
def_intensities_dl[:,t, i] = intercept_int_def' .* macro_int_def[:, t] .* def_intensities_dl[:, t, i]
oe_intensities_dl[:,t, i] = intercept_int_oe' .* macro_int_oe[:, t] .* oe_intensities_dl[:, t, i]
end
end
end
time_intensities = toc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment