mitsuba2 icon indicating copy to clipboard operation
mitsuba2 copied to clipboard

[❔ other question] Memory leakage when doing masked assignment

Open saeedhd96 opened this issue 3 years ago • 1 comments

Summary

Strange memory leakage in case of masked assignment!

System configuration

I am using a version of broken branch!

Description

I have defined a method called sample2 which is inside an integrator. This method is supposed to do Witthed-style ray tracing and return the resulting surface interaction. No need to read all the contents of the code to answer my question. The strange behavior of memory leakage happens at one of the lines of this code when I do throughput[active] *= bsdf_val;! That line is just a normal masked assignment that is used in any integrator sample methods. It is shown in the code bellow under the only commented line. Would you mind helping me figure out the reason?

By memory leakage I mean, memory is allocated and not freed, so that over time, the accumalated unused memory causes shortage of memory. Once I comment out this line, the problem doesn't exist anymore.

Your time is appreciated.

  static std::pair<Spectrum, SurfaceInteraction3f> sample2(const Scene *scene,
                                     Sampler *sampler,
                                     const SurfaceInteraction3f &si_,
                                     const Medium * /* medium */,
                                     Float * /* aovs */,
                                     Mask active, int size)  {


        Spectrum throughput(1.f);       
        SurfaceInteraction3f si(si_);
        SurfaceInteraction3f final_si(si);

        Mask specs = is_specular(si);
        Mask non_specs = si.is_valid() & ~specs;
        si_masked_assignment(final_si, si, non_specs);


        
        for (int i = 0; i < 5; i++){
             if (ek::none(specs)){ return { throughput, final_si };}
             
            BSDFContext ctx;
            BSDFPtr bsdf = si.bsdf();
            active = specs;            
            auto [bs, bsdf_val] = bsdf->sample(ctx, si, sampler->next_1d(active),
                                               sampler->next_2d(active), active);
            bsdf_val = si.to_world_mueller(bsdf_val, -bs.wo, si.wi);

//         The following line causes a memory leakage and if I comment it out, there will be no issue! Even if I used ek::select for this, the problem would persist!
            throughput[active] *= bsdf_val;


            RayDifferential3f ray = si.spawn_ray(si.to_world(bs.wo));
            SurfaceInteraction3f si_bsdf = scene->ray_intersect(ray, active);
            si = si_bsdf;
            specs = is_specular(si);
            non_specs = si.is_valid() & ~specs;            
            si_masked_assignment(final_si, si, non_specs);                        


        } 

                                             

        return { throughput, final_si };
    }

static Mask is_specular(SurfaceInteraction3f &si){
    
    BSDFPtr bsdf = si.bsdf();
    return has_flag(bsdf->flags(), BSDFFlags::DeltaReflection);

}

static void si_masked_assignment(SurfaceInteraction3f &final_si, SurfaceInteraction3f &current_si, Mask current_activity)  {
    final_si.p = ek::select(current_activity, current_si.p, final_si.p); 
    final_si.wi = ek::select(current_activity, current_si.wi, final_si.wi);
    final_si.n = ek::select(current_activity, current_si.n, final_si.n);
    final_si.t = ek::select(current_activity, current_si.t, final_si.t);
    final_si.uv = ek::select(current_activity, current_si.uv, final_si.uv);     
    final_si.shape =ek::select(current_activity, current_si.shape, final_si.shape);
}

saeedhd96 avatar Jul 06 '21 06:07 saeedhd96

Hello @saeedhd96,

a version of broken branch

The broken branch is not supported, and many bugs will be fixed in the upcoming release (hopefully including the memory leak that you observe).

In the meantime, you could try the alternative formulation just to see if it helps:

// throughput[active] *= bsdf_val;
throughput = ek::select(active, throughput * bsdf_val, throughput);

merlinND avatar Jul 13 '21 13:07 merlinND