function [] = code_and_pool_batch(...
    save_dir, file_info, dict, layer_params, config)

% Create directory to save features if needed
if ~exist(save_dir, 'dir')
    mkdir(save_dir);
end

% Precompute G
G = dict'*dict;
% for i = 1:file_info.num_files
parfor i = 1:file_info.num_files
    [parent_dirs, name_root] = get_file_info_entry(file_info, i);
    save_path = fullfile(save_dir, [name_root '.mat']);

    if ~exist(save_path, 'file')
        % Read data and get patches
        [prev_feature_map, prev_map_info] = ...
            load_feature_map(parent_dirs, name_root, ...
                             layer_params, config.load_params);
        patches = get_patches(prev_feature_map, layer_params);

        % Coding and Pooling
        codes = omp_coding(patches, dict, G, layer_params);
        [feature_map, map_info] = ...
            omp_pooling(codes, layer_params, prev_map_info);

        % Save feature map and info
        % save(save_path, 'feature_map', 'map_info');
        parsave(save_path, feature_map, map_info);
    end
end
end

% function so that we can call save from parfor.
function parsave(fname, feature_map, map_info)
  save(fname, 'feature_map', 'map_info');
end
