Esempio n. 1
0
    def get_breakpoints(reader,
                        time_mode,
                        time_interval=-1,
                        num_of_pixel_of_time_dim=-1,
                        **kwargs):
        """
        retrieve the breakpoints given time_mode and time_interval or num_of_pixel_of_time_dim,
        break point breaks the trace into chunks of given time_interval

        :param reader: reader for reading trace
        :param time_mode: either real time (r) or virtual time (v)
        :param time_interval: the intended time_interval of data chunk
        :param num_of_pixel_of_time_dim: the number of chunks, this is used when it is hard to estimate time_interval,
                                you only need specify one, either num_of_pixel_of_time_dim or time_interval
        :param kwargs: not used now
        :return: a numpy list of break points begin with 0, ends with total_num_requests
        """

        assert time_interval != -1 or num_of_pixel_of_time_dim != -1, \
            "please provide at least one parameter, time_interval or num_of_pixel_of_time_dim"
        return c_heatmap.get_breakpoints(
            reader.c_reader,
            time_mode=time_mode,
            time_interval=time_interval,
            num_of_pixel_of_time_dim=num_of_pixel_of_time_dim)
Esempio n. 2
0
    def __init__(self, cache_size, reader, **kwargs):
        super().__init__(cache_size, **kwargs)

        self.reader = reader
        self.reader.lock.acquire()
        self.next_access = c_heatmap.get_next_access_dist(self.reader.c_reader)
        self.reader.lock.release()
        self.pq = heapdict()
        self.ts = 0
Esempio n. 3
0
    def test1_vReader(self):
        reader = VscsiReader("{}/trace.vscsi".format(DAT_FOLDER))

        bpr = get_breakpoints(reader, 'r', time_interval=1000000)
        self.assertEqual(bpr[10], 53)
        bpr = get_breakpoints(reader, 'r', num_of_pixel_of_time_dim=1000)
        self.assertEqual(bpr[10], 245)
        bpv = get_breakpoints(reader, 'v', time_interval=1000)
        self.assertEqual(bpv[10], 10000)

        c_next_access = c_heatmap.get_next_access_dist(reader.c_reader)
        py_next_access = get_next_access_dist(reader)
        self.assertListEqual(list(c_next_access), list(py_next_access))
    def test1_vReader(self):
        reader = VscsiReader("{}/trace.vscsi".format(DAT_FOLDER))

        bpr = get_breakpoints(reader, 'r', time_interval=1000000)
        self.assertEqual(bpr[10], 53)
        bpr = get_breakpoints(reader, 'r', num_of_pixel_of_time_dim=1000)
        self.assertEqual(bpr[10], 245)
        bpv = get_breakpoints(reader, 'v', time_interval=1000)
        self.assertEqual(bpv[10], 10000)

        c_next_access = c_heatmap.get_next_access_dist(reader.c_reader)
        py_next_access = get_next_access_dist(reader)
        self.assertListEqual(list(c_next_access), list(py_next_access))
Esempio n. 5
0
    def get_breakpoints(reader, time_mode,
                        time_interval=-1,
                        num_of_pixel_of_time_dim=-1,
                        **kwargs):
        """
        retrieve the breakpoints given time_mode and time_interval or num_of_pixel_of_time_dim,
        break point breaks the trace into chunks of given time_interval

        :param reader: reader for reading trace
        :param time_mode: either real time (r) or virtual time (v)
        :param time_interval: the intended time_interval of data chunk
        :param num_of_pixel_of_time_dim: the number of chunks, this is used when it is hard to estimate time_interval,
                                you only need specify one, either num_of_pixel_of_time_dim or time_interval
        :param kwargs: not used now
        :return: a numpy list of break points begin with 0, ends with total_num_requests
        """

        assert time_interval != -1 or num_of_pixel_of_time_dim != -1, \
            "please provide at least one parameter, time_interval or num_of_pixel_of_time_dim"
        return c_heatmap.get_breakpoints(reader.c_reader, time_mode=time_mode,
                                                    time_interval=time_interval,
                                                    num_of_pixel_of_time_dim=num_of_pixel_of_time_dim)
Esempio n. 6
0
    def heatmap(self,
                reader,
                time_mode,
                plot_type,
                algorithm="LRU",
                time_interval=-1,
                num_of_pixel_of_time_dim=-1,
                cache_params=None,
                **kwargs):
        """

        :param cache_params:
        :param num_of_pixel_of_time_dim:
        :param time_interval:
        :param algorithm:
        :param plot_type:
        :param time_mode:
        :param reader:
        :param kwargs: include num_of_threads, figname, ewma_coefficient (default: 0.2)
        :return:
        """

        DEFAULT_EWMA_COEFFICIENT = 0.2
        # assert time_interval != -1 or num_of_pixel_of_time_dim != -1, \
        #     "please provide at least one parameter, time_interval or num_of_pixel_of_time_dim"
        reader.reset()

        cache_size = kwargs.get("cache_size", -1)
        figname = kwargs.get("figname", "heatmap.png")
        num_of_threads = kwargs.get("num_of_threads", os.cpu_count())
        assert time_mode in ["r", "v"], "Cannot recognize this time_mode, "\
                                        "it can only be either real time(r) or virtual time(v), " \
                                        "but you give {}".format(time_mode)

        if plot_type == "hit_ratio_start_time_end_time" or plot_type == "hr_st_et":
            assert cache_size != -1, "please provide cache_size parameter for plotting hr_st_et"
            # this is used to specify the type of hit ratio for each pixel, when it is False, it means
            # the hit ratio is the overall hit ratio from the beginning of trace,
            # if True, then the hit ratio will be exponentially decayed moving average hit ratio from beginning plus
            # the hit ratio in current time interval, the ewma_coefficient specifies the ratio of
            # history hit ratio in the calculation
            enable_ihr = False
            ewma_coefficient = DEFAULT_EWMA_COEFFICIENT
            if 'interval_hit_ratio' in kwargs or "enable_ihr" in kwargs:
                enable_ihr = kwargs.get("interval_hit_ratio",
                                        False) or kwargs.get(
                                            "enable_ihr", False)
                ewma_coefficient = kwargs.get("ewma_coefficient",
                                              DEFAULT_EWMA_COEFFICIENT)

            if algorithm.lower() in const.C_AVAIL_CACHE:
                xydict = c_heatmap.heatmap(
                    reader.c_reader,
                    time_mode,
                    plot_type,
                    cache_size,
                    algorithm,
                    interval_hit_ratio=enable_ihr,
                    ewma_coefficient=ewma_coefficient,
                    time_interval=time_interval,
                    num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                    cache_params=cache_params,
                    num_of_threads=num_of_threads)
            else:
                raise RuntimeError(
                    "haven't provided support given algorithm in C yet: " +
                    str(algorithm))
                pass

            text = "      " \
                   "cache size: {},\n      cache type: {},\n      " \
                   "time type:  {},\n      time interval: {},\n      " \
                   "plot type: \n{}".format(cache_size,
                                            algorithm, time_mode, time_interval, plot_type)

            # coordinate to put the text
            x1, y1 = xydict.shape
            x1 = int(x1 / 2.8)
            y1 /= 8

            xlabel = 'Start Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "End Time ({})".format(time_mode)
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            # yticks = ticker.FuncFormatter(lambda x, _: int(x*bin_size))
            ax = plt.gca()
            ax.text(x1, y1, text)  # , fontsize=20)  , color='blue')

            if not figname:
                figname = '_'.join([algorithm,
                                    str(cache_size), plot_type]) + '.png'

            plot_data = np.ma.array(xydict,
                                    mask=np.tri(len(xydict), dtype=int).T)
            self.draw_heatmap(plot_data,
                              figname=figname,
                              fixed_range=(0, 1),
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks)

        elif plot_type == "hr_interval_size":
            assert cache_size != -1, "please provide cache_size parameter for plotting hr_st_et"
            ewma_coefficient = kwargs.get("ewma_coefficient",
                                          DEFAULT_EWMA_COEFFICIENT)
            bin_size = kwargs.get("bin_size",
                                  cache_size // DEF_NUM_BIN_PROF + 1)

            xydict = c_heatmap.heatmap(
                reader.c_reader,
                time_mode,
                plot_type,
                cache_size,
                algorithm,
                ewma_coefficient=ewma_coefficient,
                bin_size=bin_size,
                time_interval=time_interval,
                num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                cache_params=cache_params,
                num_of_threads=num_of_threads)

            xlabel = 'Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Cache Size"
            yticks = ticker.FuncFormatter(lambda x, _: int(x * bin_size))
            title = "Interval Hit Ratio With Size (ewma {})".format(
                ewma_coefficient)

            plot_data = xydict

            if not figname:
                figname = '_'.join([algorithm,
                                    str(cache_size), plot_type]) + '.png'
            self.draw_heatmap(plot_data,
                              figname=figname,
                              fixed_range=(0, 1),
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title)

        elif plot_type == "hit_ratio_start_time_cache_size" or plot_type == "hr_st_size":
            pass

        elif plot_type == "avg_rd_start_time_end_time" or plot_type == "avg_rd_st_et":
            pass

        elif plot_type == "cold_miss_count_start_time_end_time":
            raise RuntimeError("this plot is deprecated")

        elif plot_type == "???":
            pass

        elif plot_type == "rd_distribution":
            if not figname:
                figname = 'rd_distribution.png'

            xydict, log_base = c_heatmap.\
                hm_rd_distribution(reader.c_reader, time_mode,
                                   time_interval=time_interval,
                                   num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                   num_of_threads=num_of_threads)

            title = "Reuse Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Reuse Distance"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "rd_distribution_CDF":
            if not figname:
                figname = 'rd_distribution_CDF.png'

            xydict, log_base = c_heatmap.\
                hm_rd_distribution(reader.c_reader, time_mode,
                                   time_interval=time_interval,
                                   num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                   num_of_threads=num_of_threads, CDF=1)

            title = "Reuse Distance Distribution CDF Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Reuse Distance (CDF)"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "future_rd_distribution":
            if not figname:
                figname = 'future_rd_distribution.png'

            xydict, log_base = c_heatmap.\
                hm_future_rd_distribution(reader.c_reader, time_mode,
                                          time_interval=time_interval,
                                          num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                          num_of_threads=num_of_threads)

            title = "Future Reuse Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Future Reuse Distance"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "dist_distribution":
            if not figname:
                figname = 'dist_distribution.png'

            xydict, log_base = c_heatmap.\
                hm_dist_distribution(reader.c_reader, time_mode,
                                     time_interval=time_interval,
                                     num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                     num_of_threads=num_of_threads)

            title = "Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Distance"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_dist' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_dist'] > 0, "filter_dist must be positive"
                index_pos = int(
                    np.log(kwargs['filter_dist']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "reuse_time_distribution":
            if not figname:
                figname = 'rt_distribution.png'

            xydict, log_base = c_heatmap.\
                hm_reuse_time_distribution(reader.c_reader, time_mode,
                                           time_interval=time_interval,
                                           num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                           num_of_threads=num_of_threads)

            title = "Reuse Time Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Reuse Time"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rt' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rt'] > 0, "filter_rt must be positive"
                index_pos = int(np.log(kwargs['filter_rt']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        else:
            raise RuntimeError(
                "Cannot recognize this plot type, "
                "please check documentation, yoru input is {}".format(
                    plot_type))

        reader.reset()
Esempio n. 7
0
    def diff_heatmap(self,
                     reader,
                     time_mode,
                     plot_type,
                     algorithm1,
                     time_interval=-1,
                     num_of_pixel_of_time_dim=-1,
                     algorithm2="Optimal",
                     cache_params1=None,
                     cache_params2=None,
                     **kwargs):
        """

        :param time_interval:
        :param num_of_pixel_of_time_dim:
        :param algorithm2:
        :param cache_params1:
        :param cache_params2:
        :param algorithm1:
        :param plot_type:
        :param time_mode:
        :param reader:
        :param kwargs: include num_of_process, figname
        :return:
        """

        reader.reset()

        cache_size = kwargs.get("cache_size", -1)
        figname = kwargs.get(
            "figname", '_'.join(
                [algorithm2 + '-' + algorithm1,
                 str(cache_size), plot_type]) + '.png')
        num_of_threads = kwargs.get("num_of_threads", os.cpu_count())
        assert time_mode in ["r", "v"], "Cannot recognize time_mode {}, it can only be either " \
                                        "real time(r) or virtual time(v)".format(time_mode)

        if plot_type == "hit_ratio_start_time_end_time" or plot_type == "hr_st_et":
            assert cache_size != -1, "please provide cache_size for plotting hr_st_et"

            xydict = c_heatmap.diff_heatmap(
                reader.c_reader,
                time_mode,
                "hr_st_et",
                cache_size,
                algorithm1,
                algorithm2,
                time_interval=time_interval,
                num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                cache_params1=cache_params1,
                cache_params2=cache_params2,
                num_of_threads=num_of_threads)

            text = "      differential heatmap\n      cache size: {},\n      cache type: ({}-{})/{},\n" \
                   "      time type: {},\n      time interval: {},\n      plot type: \n{}".format(
                cache_size, algorithm2, algorithm1, algorithm1, time_mode, time_interval, plot_type)

            x1, y1 = xydict.shape
            x1 = int(x1 / 2.8)
            y1 /= 8

            title = "Differential Heatmap"
            xlabel = 'Start Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "End Time ({})".format(time_mode)
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ax = plt.gca()
            ax.text(x1, y1, text)

            self.draw_heatmap(xydict,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              fixed_range=(-1, 1))

        elif plot_type == "hit_ratio_start_time_cache_size":
            pass

        elif plot_type == "avg_rd_start_time_end_time":
            pass

        elif plot_type == "cold_miss_count_start_time_end_time":
            print("this plot is discontinued")

        elif plot_type == "???":
            pass

        else:
            raise RuntimeError(
                "Cannot recognize this plot type, please check documentation, yoru input is %s"
                % time_mode)

        reader.reset()
Esempio n. 8
0
    def heatmap(self, reader, time_mode, plot_type,
                algorithm="LRU",
                time_interval=-1,
                num_of_pixel_of_time_dim=-1,
                cache_params=None,
                **kwargs):
        """

        This functions provides different types of heatmap plotting

        :param reader: the reader instance for data input
        :param time_mode: either real time (r) or virtual time (v),
                    real time is wall clock time, it needs the reader containing real time info
                    virtual time is the reference number, aka. the number of requests
        :param plot_type: different types of heatmap, supported heatmaps are listed in the table below
        :param algorithm: cache replacement algorithm (default: LRU)
        :param time_interval: the time interval of each pixel
        :param num_of_pixel_of_time_dim: if don't want to specify time_interval, you can also specify how many pixels you want
        :param cache_params: params used in cache
        :param kwargs: include num_of_threads, figname, enable_ihr, ema_coef(default: 0.8), info_on_fig
        :return:


        ============================  ========================  ===========================================================================
        plot_type                       required parameters         descriptions
        ============================  ========================  ===========================================================================
        "hr_st_et"                      cache_size              hit ratio with regarding to start time (x) and end time (y)
        "hr_st_size"                    NOT IMPLEMENTED         hit ratio with reagarding to start time (x) and size (y)
        "avg_rd_st_et"                  NOT IMPLEMENTED         average reuse distance with regaarding to start time (x) and end time (y)
        "rd_distribution"               N/A                     reuse distance distribution (y) vs time (x)
        "rd_distribution_CDF"           N/A                     reuse distance distribution CDF (y) vs time (x)
        "future_rd_distribution"        N/A                     future reuse distance distribution (y) vs time (x)
        "dist_distribution"             N/A                     absolute distance distribution (y) vs time (x)
        "rt_distribution"               N/A                     reuse time distribution (y) vs time (x)
        ============================  ========================  ===========================================================================


        """

        # assert time_interval != -1 or num_of_pixel_of_time_dim != -1, \
        #     "please provide at least one parameter, time_interval or num_of_pixel_of_time_dim"
        reader.reset()

        cache_size = kwargs.get("cache_size", -1)
        figname = kwargs.get("figname", "CHeatmap_{}.png".format(plot_type))
        num_of_threads = kwargs.get("num_of_threads", os.cpu_count())
        if cache_params is None: cache_params = {}
        plot_kwargs = kwargs.get("plot_kwargs", {"figname": figname})
        assert time_mode in ["r", "v"], "Cannot recognize this time_mode, "\
                                        "it can only be either real time(r) or virtual time(v), " \
                                        "but you give {}".format(time_mode)
        assert algorithm.lower() in const.C_AVAIL_CACHE, \
            "No support for given algorithm in CMimircache, please try PyHeatmap" + str(algorithm)


        if plot_type == "hit_ratio_start_time_end_time" or plot_type == "hr_st_et":
            assert cache_size != -1, "please provide cache_size parameter for plotting hr_st_et"
            # this is used to specify the type of hit ratio for each pixel, when it is False, it means
            # the hit ratio is the overall hit ratio from the beginning of trace,
            # if True, then the hit ratio will be exponentially decayed moving average hit ratio from beginning plus
            # the hit ratio in current time interval, the ewma_coefficient specifies the ratio of
            # history hit ratio in the calculation
            enable_ihr = False
            ema_coef  = DEF_EMA_HISTORY_WEIGHT
            if 'interval_hit_ratio'in kwargs or "enable_ihr" in kwargs:
                enable_ihr = kwargs.get("interval_hit_ratio", False) or kwargs.get("enable_ihr", False)
                ema_coef  = kwargs.get("ema_coef", DEF_EMA_HISTORY_WEIGHT)

            xydict = c_heatmap.heatmap(reader.c_reader, time_mode, plot_type,
                                                  cache_size, algorithm,
                                                  interval_hit_ratio=enable_ihr,
                                                  ewma_coefficient=ema_coef,
                                                  time_interval=time_interval,
                                                  num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                                  cache_params=cache_params,
                                                  num_of_threads=num_of_threads)


            text = "cache size: {},\ncache type: {},\n" \
                   "time type:  {},\ntime interval: {},\n" \
                   "plot type: {}".format(cache_size, algorithm, time_mode, time_interval, plot_type)

            # coordinate to put the text
            x1, y1 = xydict.shape
            x1, y1 = int(x1 / 2), y1 / 8

            xlabel = 'Start Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "End Time ({})".format(time_mode)
            yticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            # yticks = ticker.FuncFormatter(lambda x, _: int(x*bin_size))
            ax = plt.gca()
            if kwargs.get("info_on_fig", True):
                ax.text(x1, y1, text)  # , fontsize=20)  , color='blue')

            plot_data = np.ma.array(xydict, mask=np.tri(len(xydict), dtype=int).T)
            self.draw_heatmap(plot_data, figname=figname, fixed_range=(0, 1),
                              xlabel=xlabel, ylabel=ylabel, xticks=xticks, yticks=yticks)


        elif plot_type == "hit_ratio_start_time_cache_size" or plot_type == "hr_st_size":
            assert cache_size != -1, "please provide cache_size parameter for plotting ihr_st_size"

            enable_ihr = False
            ema_coef  = DEF_EMA_HISTORY_WEIGHT
            bin_size = kwargs.get("bin_size", cache_size // DEF_NUM_BIN_PROF + 1)
            if 'interval_hit_ratio'in kwargs or "enable_ihr" in kwargs:
                enable_ihr = kwargs.get("interval_hit_ratio", False) or kwargs.get("enable_ihr", False)
                ema_coef  = kwargs.get("ema_coef", DEF_EMA_HISTORY_WEIGHT)

            # a temporary fix as hr_st_size is not implemented in C, only hr_st_size with enable_ihr

            if not enable_ihr:
                raise RuntimeError("NOT Implemented")

            else:
                plot_type = "hr_interval_size"


            xydict = c_heatmap.heatmap(reader.c_reader, time_mode, plot_type,
                                                  cache_size, algorithm,
                                                  ewma_coefficient=ema_coef,
                                                  bin_size = bin_size,
                                                  time_interval=time_interval,
                                                  num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                                  cache_params=cache_params,
                                                  num_of_threads=num_of_threads)

            xlabel = 'Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "Cache Size"
            yticks = ticker.FuncFormatter(lambda x, _: int(x*bin_size))

            plot_data = xydict
            self.draw_heatmap(plot_data, figname=figname, fixed_range=(0, 1),
                              xlabel=xlabel, ylabel=ylabel, xticks=xticks, yticks=yticks)



        elif plot_type == "effective_size":
            assert cache_size != -1, "please provide cache_size parameter for plotting effective_size"

            bin_size = kwargs.get("bin_size", cache_size // DEF_NUM_BIN_PROF + 1)
            use_percent = kwargs.get("use_percent", False)
            xydict = c_heatmap.heatmap(reader.c_reader, time_mode, plot_type,
                                                  cache_size, algorithm,
                                                  bin_size = bin_size,
                                                  time_interval=time_interval,
                                                  num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                                  cache_params=None,
                                                  use_percent=use_percent,
                                                  num_of_threads=num_of_threads)

            plot_kwargs["xlabel"]  = plot_kwargs.get("xlabel", 'Time ({})'.format(time_mode))
            plot_kwargs["xticks"]  = plot_kwargs.get("xticks", ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1))))
            plot_kwargs["ylabel"]  = plot_kwargs.get("ylabel", "Cache Size")
            plot_kwargs["yticks"]  = plot_kwargs.get("yticks", ticker.FuncFormatter(lambda x, _: int(x*bin_size)))
            plot_kwargs["imshow_kwargs"] = {"cmap": "Oranges"}
            if use_percent:
                plot_kwargs["imshow_kwargs"].update({"vmin": 0, "vmax":1})

            plot_data = xydict
            draw_heatmap(plot_data, **plot_kwargs)



        elif plot_type == "avg_rd_start_time_end_time" or plot_type == "avg_rd_st_et":
            raise RuntimeError("NOT Implemented")


        elif plot_type == "cold_miss_count_start_time_end_time":
            raise RuntimeError("this plot is deprecated")


        elif plot_type == "rd_distribution":
            xydict, log_base = c_heatmap.\
                hm_rd_distribution(reader.c_reader, time_mode,
                                   time_interval=time_interval,
                                   num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                   num_of_threads=num_of_threads)



            title = "Reuse Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode=="r" else "Virtual")
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "Reuse Distance"
            yticks = ticker.FuncFormatter(lambda x, _: '{:2.0f}'.format(log_base ** x-1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data==0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd'])/np.log(log_base))
                plot_data[:index_pos+1, :] = 0


            self.draw_heatmap(plot_data, figname=figname, xlabel=xlabel, ylabel=ylabel,
                              xticks=xticks, yticks=yticks, title=title,
                              imshow_kwargs=imshow_kwargs)


        elif plot_type == "rd_distribution_CDF":
            xydict, log_base = c_heatmap.\
                hm_rd_distribution(reader.c_reader, time_mode,
                                   time_interval=time_interval,
                                   num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                   num_of_threads=num_of_threads, CDF=1)

            title = "Reuse Distance Distribution CDF Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode=="r" else "Virtual")
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "Reuse Distance (CDF)"
            yticks = ticker.FuncFormatter(lambda x, _: '{:2.0f}'.format(log_base ** x-1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data==0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd'])/np.log(log_base))
                plot_data[:index_pos+1, :] = 0


            self.draw_heatmap(plot_data, figname=figname, xlabel=xlabel, ylabel=ylabel,
                              xticks=xticks, yticks=yticks, title=title,
                              imshow_kwargs=imshow_kwargs)


        elif plot_type == "future_rd_distribution":
            xydict, log_base = c_heatmap.\
                hm_future_rd_distribution(reader.c_reader, time_mode,
                                          time_interval=time_interval,
                                          num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                          num_of_threads=num_of_threads)

            title = "Future Reuse Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode=="r" else "Virtual")
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "Future Reuse Distance"
            yticks = ticker.FuncFormatter(lambda x, _: '{:2.0f}'.format(log_base ** x-1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data==0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd'])/np.log(log_base))
                plot_data[:index_pos+1, :] = 0


            self.draw_heatmap(plot_data, figname=figname, xlabel=xlabel, ylabel=ylabel,
                              xticks=xticks, yticks=yticks, title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "dist_distribution":
            xydict, log_base = c_heatmap.\
                hm_dist_distribution(reader.c_reader, time_mode,
                                     time_interval=time_interval,
                                     num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                     num_of_threads=num_of_threads)

            title = "Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode=="r" else "Virtual")
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "Distance"
            yticks = ticker.FuncFormatter(lambda x, _: '{:2.0f}'.format(log_base ** x-1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data==0, plot_data, copy=False)

            if 'filter_dist' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_dist'] > 0, "filter_dist must be positive"
                index_pos = int(np.log(kwargs['filter_dist'])/np.log(log_base))
                plot_data[:index_pos+1, :] = 0


            self.draw_heatmap(plot_data, figname=figname, xlabel=xlabel, ylabel=ylabel,
                              xticks=xticks, yticks=yticks, title=title,
                              imshow_kwargs=imshow_kwargs)


        elif plot_type == "reuse_time_distribution" or plot_type == "rt_distribution":
            xydict, log_base = c_heatmap.\
                hm_reuse_time_distribution(reader.c_reader, time_mode,
                                           time_interval=time_interval,
                                           num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                           num_of_threads=num_of_threads)

            title = "Reuse Time Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode=="r" else "Virtual")
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "Reuse Time"
            yticks = ticker.FuncFormatter(lambda x, _: '{:2.0f}'.format(log_base ** x-1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data==0, plot_data, copy=False)

            if 'filter_rt' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rt'] > 0, "filter_rt must be positive"
                index_pos = int(np.log(kwargs['filter_rt'])/np.log(log_base))
                plot_data[:index_pos+1, :] = 0


            self.draw_heatmap(plot_data, figname=figname, xlabel=xlabel, ylabel=ylabel,
                              xticks=xticks, yticks=yticks, title=title,
                              imshow_kwargs=imshow_kwargs)

        else:
            raise RuntimeError("Cannot recognize this plot type, "
                "please check documentation, yoru input is {}".format(plot_type))


        reader.reset()
Esempio n. 9
0
    def diff_heatmap(self, reader, time_mode, plot_type, algorithm1,
                     time_interval=-1, num_of_pixel_of_time_dim=-1, algorithm2="Optimal",
                     cache_params1=None, cache_params2=None, **kwargs):
        """

        :param time_interval:
        :param num_of_pixel_of_time_dim:
        :param algorithm2:
        :param cache_params1:
        :param cache_params2:
        :param algorithm1:
        :param plot_type:
        :param time_mode:
        :param reader:
        :param kwargs: include num_of_process, figname
        :return:
        """

        reader.reset()

        cache_size = kwargs.get("cache_size", -1)
        figname = kwargs.get("figname", '_'.join([algorithm2 + '-' + algorithm1, str(cache_size), plot_type]) + '.png')
        num_of_threads = kwargs.get("num_of_threads", os.cpu_count())
        assert time_mode in ["r", "v"], "Cannot recognize time_mode {}, it can only be either " \
                                        "real time(r) or virtual time(v)".format(time_mode)

        if plot_type == "hit_ratio_start_time_end_time" or plot_type == "hr_st_et":
            assert cache_size != -1, "please provide cache_size for plotting hr_st_et"

            xydict = c_heatmap.diff_heatmap(reader.c_reader, time_mode,
                                                       "hr_st_et", cache_size,
                                                       algorithm1, algorithm2,
                                                       time_interval=time_interval,
                                                       num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                                       cache_params1=cache_params1,
                                                       cache_params2=cache_params2,
                                                       num_of_threads=num_of_threads)

            text = "differential heatmap\ncache size: {},\ncache type: ({}-{})/{},\n" \
                   "time type: {},\ntime interval: {},\nplot type: {}".format(
                cache_size, algorithm2, algorithm1, algorithm1, time_mode, time_interval, plot_type)

            x1, y1 = xydict.shape
            x1, y1 = int(x1 / 2.4), y1/8

            title = "Differential Heatmap"
            xlabel = 'Start Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ylabel = "End Time ({})".format(time_mode)
            yticks = ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (xydict.shape[1]-1)))
            ax = plt.gca()
            ax.text(x1, y1, text)


            self.draw_heatmap(xydict, figname=figname, xlabel=xlabel, ylabel=ylabel,
                              xticks=xticks, yticks=yticks, title=title, fixed_range=(-1, 1))



        elif plot_type == "hit_ratio_start_time_cache_size":
            pass


        elif plot_type == "avg_rd_start_time_end_time":
            pass


        elif plot_type == "cold_miss_count_start_time_end_time":
            print("this plot is discontinued")


        elif plot_type == "???":
            pass



        else:
            raise RuntimeError(
                "Cannot recognize this plot type, please check documentation, yoru input is %s" % time_mode)

        reader.reset()
Esempio n. 10
0
    def diff_heatmap(self, time_mode, plot_type, algorithm1="LRU", time_interval=-1, num_of_pixels=-1,
                     algorithm2="Optimal", cache_params1=None, cache_params2=None, cache_size=-1, **kwargs):
        """
        Plot the differential heatmap between two algorithms by alg2 - alg1

        :param cache_size: size of cache
        :param time_mode: time time_mode "v" for virutal time, "r" for real time
        :param plot_type: same as the name in heatmap function
        :param algorithm1:  name of the first alg
        :param time_interval: same as in heatmap
        :param num_of_pixels: same as in heatmap
        :param algorithm2: name of the second algorithm
        :param cache_params1: parameters of the first algorithm
        :param cache_params2: parameters of the second algorithm
        :param kwargs: include num_of_threads
        """

        figname = kwargs.get("figname", 'differential_heatmap.png')
        num_of_threads = kwargs.get("num_of_threads", DEF_NUM_THREADS)
        assert self.reader is not None, "you haven't opened a trace yet"
        assert cache_size != -1, "you didn't provide size for cache"
        assert cache_size <= self.num_of_req(), \
                    "you cannot specify cache size({}) larger than " \
                    "trace length({})".format(cache_size, self.num_of_req())


        if algorithm1.lower() in C_AVAIL_CACHE and algorithm2.lower() in C_AVAIL_CACHE:
            hm = CHeatmap()
            hm.diff_heatmap(self.reader, time_mode, plot_type,
                            cache_size=cache_size,
                            time_interval=time_interval,
                            num_of_pixels=num_of_pixels,
                            algorithm1=CACHE_NAME_CONVRETER[algorithm1.lower()],
                            algorithm2=CACHE_NAME_CONVRETER[algorithm2.lower()],
                            cache_params1=cache_params1,
                            cache_params2=cache_params2,
                            **kwargs)

        else:
            hm = PyHeatmap()
            if algorithm1.lower() not in C_AVAIL_CACHE:
                xydict1 = hm.compute_heatmap(self.reader, time_mode, plot_type,
                                                   time_interval=time_interval,
                                                   cache_size=cache_size,
                                                   algorithm=algorithm1,
                                                   cache_params=cache_params1,
                                                   **kwargs)[0]
            else:
                xydict1 = c_heatmap.heatmap(self.reader.c_reader, time_mode, plot_type,
                                                       cache_size=cache_size,
                                                       time_interval=time_interval,
                                                       algorithm=algorithm1,
                                                       cache_params=cache_params1,
                                                       num_of_threads=num_of_threads)

            if algorithm2.lower() not in C_AVAIL_CACHE:
                xydict2 = hm.compute_heatmap(self.reader, time_mode, plot_type,
                                                   time_interval=time_interval,
                                                   cache_size=cache_size,
                                                   algorithm=algorithm2,
                                                   cache_params=cache_params2,
                                                   **kwargs)[0]
            else:
                xydict2 = c_heatmap.heatmap(self.reader.c_reader, time_mode, plot_type,
                                                       time_interval=time_interval,
                                                       cache_size=cache_size,
                                                       algorithm=algorithm2,
                                                       cache_params=cache_params2,
                                                       num_of_threads=num_of_threads)

            text = "differential heatmap\ncache size: {},\ncache type: ({}-{})/{},\n" \
                   "time type: {},\ntime interval: {},\nplot type: \n{}".format(
                cache_size, algorithm2, algorithm1, algorithm1, time_mode, time_interval, plot_type)

            x1, y1 = xydict1.shape
            x1, y1 = int(x1 / 2.8), y1/8
            ax = plt.gca()
            ax.text(x1, y1, text)

            np.seterr(divide='ignore', invalid='ignore')
            plot_data = (xydict2 - xydict1) / xydict1
            plot_data = np.ma.array(plot_data, mask=np.tri(len(plot_data), k=-1, dtype=int).T)

            plot_kwargs = {"figname": figname}
            plot_kwargs["xlabel"]  = plot_kwargs.get("xlabel", 'Start Time ({})'.format(time_mode))
            plot_kwargs["xticks"]  = plot_kwargs.get("xticks", ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (plot_data.shape[1]-1))))
            plot_kwargs["ylabel"]  = plot_kwargs.get("ylabel", "End Time ({})".format(time_mode))
            plot_kwargs["yticks"]  = plot_kwargs.get("yticks", ticker.FuncFormatter(lambda x, _: '{:.0%}'.format(x / (plot_data.shape[0]-1))))
            plot_kwargs["imshow_kwargs"] = {"vmin": -1, "vmax": 1}


            draw_heatmap(plot_data, **plot_kwargs)
Esempio n. 11
0
    def heatmap(self,
                reader,
                time_mode,
                plot_type,
                algorithm="LRU",
                time_interval=-1,
                num_of_pixel_of_time_dim=-1,
                cache_params=None,
                **kwargs):
        """

        This functions provides different types of heatmap plotting

        :param reader: the reader instance for data input
        :param time_mode: either real time (r) or virtual time (v),
                    real time is wall clock time, it needs the reader containing real time info
                    virtual time is the reference number, aka. the number of requests
        :param plot_type: different types of heatmap, supported heatmaps are listed in the table below
        :param algorithm: cache replacement algorithm (default: LRU)
        :param time_interval: the time interval of each pixel
        :param num_of_pixel_of_time_dim: if don't want to specify time_interval, you can also specify how many pixels you want
        :param cache_params: params used in cache
        :param kwargs: include num_of_threads, figname, enable_ihr, ema_coef(default: 0.8), info_on_fig
        :return:


        ============================  ========================  ===========================================================================
        plot_type                       required parameters         descriptions
        ============================  ========================  ===========================================================================
        "hr_st_et"                      cache_size              hit ratio with regarding to start time (x) and end time (y)
        "hr_st_size"                    NOT IMPLEMENTED         hit ratio with reagarding to start time (x) and size (y)
        "avg_rd_st_et"                  NOT IMPLEMENTED         average reuse distance with regaarding to start time (x) and end time (y)
        "rd_distribution"               N/A                     reuse distance distribution (y) vs time (x)
        "rd_distribution_CDF"           N/A                     reuse distance distribution CDF (y) vs time (x)
        "future_rd_distribution"        N/A                     future reuse distance distribution (y) vs time (x)
        "dist_distribution"             N/A                     absolute distance distribution (y) vs time (x)
        "rt_distribution"               N/A                     reuse time distribution (y) vs time (x)
        ============================  ========================  ===========================================================================


        """

        # assert time_interval != -1 or num_of_pixel_of_time_dim != -1, \
        #     "please provide at least one parameter, time_interval or num_of_pixel_of_time_dim"
        reader.reset()

        cache_size = kwargs.get("cache_size", -1)
        figname = kwargs.get("figname", "CHeatmap_{}.png".format(plot_type))
        num_of_threads = kwargs.get("num_of_threads", os.cpu_count())
        if cache_params is None: cache_params = {}
        plot_kwargs = kwargs.get("plot_kwargs", {"figname": figname})
        assert time_mode in ["r", "v"], "Cannot recognize this time_mode, "\
                                        "it can only be either real time(r) or virtual time(v), " \
                                        "but you give {}".format(time_mode)
        assert algorithm.lower() in const.C_AVAIL_CACHE, \
            "No support for given algorithm in CMimircache, please try PyHeatmap" + str(algorithm)

        if plot_type == "hit_ratio_start_time_end_time" or plot_type == "hr_st_et":
            assert cache_size != -1, "please provide cache_size parameter for plotting hr_st_et"
            # this is used to specify the type of hit ratio for each pixel, when it is False, it means
            # the hit ratio is the overall hit ratio from the beginning of trace,
            # if True, then the hit ratio will be exponentially decayed moving average hit ratio from beginning plus
            # the hit ratio in current time interval, the ewma_coefficient specifies the ratio of
            # history hit ratio in the calculation
            enable_ihr = False
            ema_coef = DEF_EMA_HISTORY_WEIGHT
            if 'interval_hit_ratio' in kwargs or "enable_ihr" in kwargs:
                enable_ihr = kwargs.get("interval_hit_ratio",
                                        False) or kwargs.get(
                                            "enable_ihr", False)
                ema_coef = kwargs.get("ema_coef", DEF_EMA_HISTORY_WEIGHT)

            xydict = c_heatmap.heatmap(
                reader.c_reader,
                time_mode,
                plot_type,
                cache_size,
                algorithm,
                interval_hit_ratio=enable_ihr,
                ewma_coefficient=ema_coef,
                time_interval=time_interval,
                num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                cache_params=cache_params,
                num_of_threads=num_of_threads)


            text = "cache size: {},\ncache type: {},\n" \
                   "time type:  {},\ntime interval: {},\n" \
                   "plot type: {}".format(cache_size, algorithm, time_mode, time_interval, plot_type)

            # coordinate to put the text
            x1, y1 = xydict.shape
            x1, y1 = int(x1 / 2), y1 / 8

            xlabel = 'Start Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "End Time ({})".format(time_mode)
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            # yticks = ticker.FuncFormatter(lambda x, _: int(x*bin_size))
            ax = plt.gca()
            if kwargs.get("info_on_fig", True):
                ax.text(x1, y1, text)  # , fontsize=20)  , color='blue')

            plot_data = np.ma.array(xydict,
                                    mask=np.tri(len(xydict), dtype=int).T)
            self.draw_heatmap(plot_data,
                              figname=figname,
                              fixed_range=(0, 1),
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks)

        elif plot_type == "hit_ratio_start_time_cache_size" or plot_type == "hr_st_size":
            assert cache_size != -1, "please provide cache_size parameter for plotting ihr_st_size"

            enable_ihr = False
            ema_coef = DEF_EMA_HISTORY_WEIGHT
            bin_size = kwargs.get("bin_size",
                                  cache_size // DEF_NUM_BIN_PROF + 1)
            if 'interval_hit_ratio' in kwargs or "enable_ihr" in kwargs:
                enable_ihr = kwargs.get("interval_hit_ratio",
                                        False) or kwargs.get(
                                            "enable_ihr", False)
                ema_coef = kwargs.get("ema_coef", DEF_EMA_HISTORY_WEIGHT)

            # a temporary fix as hr_st_size is not implemented in C, only hr_st_size with enable_ihr

            if not enable_ihr:
                raise RuntimeError("NOT Implemented")

            else:
                plot_type = "hr_interval_size"

            xydict = c_heatmap.heatmap(
                reader.c_reader,
                time_mode,
                plot_type,
                cache_size,
                algorithm,
                ewma_coefficient=ema_coef,
                bin_size=bin_size,
                time_interval=time_interval,
                num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                cache_params=cache_params,
                num_of_threads=num_of_threads)

            xlabel = 'Time ({})'.format(time_mode)
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Cache Size"
            yticks = ticker.FuncFormatter(lambda x, _: int(x * bin_size))

            plot_data = xydict
            self.draw_heatmap(plot_data,
                              figname=figname,
                              fixed_range=(0, 1),
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks)

        elif plot_type == "effective_size":
            assert cache_size != -1, "please provide cache_size parameter for plotting effective_size"

            bin_size = kwargs.get("bin_size",
                                  cache_size // DEF_NUM_BIN_PROF + 1)
            use_percent = kwargs.get("use_percent", False)
            xydict = c_heatmap.heatmap(
                reader.c_reader,
                time_mode,
                plot_type,
                cache_size,
                algorithm,
                bin_size=bin_size,
                time_interval=time_interval,
                num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                cache_params=None,
                use_percent=use_percent,
                num_of_threads=num_of_threads)

            plot_kwargs["xlabel"] = plot_kwargs.get(
                "xlabel", 'Time ({})'.format(time_mode))
            plot_kwargs["xticks"] = plot_kwargs.get(
                "xticks",
                ticker.FuncFormatter(
                    lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1))))
            plot_kwargs["ylabel"] = plot_kwargs.get("ylabel", "Cache Size")
            plot_kwargs["yticks"] = plot_kwargs.get(
                "yticks", ticker.FuncFormatter(lambda x, _: int(x * bin_size)))
            plot_kwargs["imshow_kwargs"] = {"cmap": "Oranges"}
            if use_percent:
                plot_kwargs["imshow_kwargs"].update({"vmin": 0, "vmax": 1})

            plot_data = xydict
            draw_heatmap(plot_data, **plot_kwargs)

        elif plot_type == "avg_rd_start_time_end_time" or plot_type == "avg_rd_st_et":
            raise RuntimeError("NOT Implemented")

        elif plot_type == "cold_miss_count_start_time_end_time":
            raise RuntimeError("this plot is deprecated")

        elif plot_type == "rd_distribution":
            xydict, log_base = c_heatmap.\
                hm_rd_distribution(reader.c_reader, time_mode,
                                   time_interval=time_interval,
                                   num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                   num_of_threads=num_of_threads)

            title = "Reuse Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Reuse Distance"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "rd_distribution_CDF":
            xydict, log_base = c_heatmap.\
                hm_rd_distribution(reader.c_reader, time_mode,
                                   time_interval=time_interval,
                                   num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                   num_of_threads=num_of_threads, CDF=1)

            title = "Reuse Distance Distribution CDF Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Reuse Distance (CDF)"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "future_rd_distribution":
            xydict, log_base = c_heatmap.\
                hm_future_rd_distribution(reader.c_reader, time_mode,
                                          time_interval=time_interval,
                                          num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                          num_of_threads=num_of_threads)

            title = "Future Reuse Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Future Reuse Distance"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rd' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rd'] > 0, "filter_rd must be positive"
                index_pos = int(np.log(kwargs['filter_rd']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "dist_distribution":
            xydict, log_base = c_heatmap.\
                hm_dist_distribution(reader.c_reader, time_mode,
                                     time_interval=time_interval,
                                     num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                     num_of_threads=num_of_threads)

            title = "Distance Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Distance"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_dist' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_dist'] > 0, "filter_dist must be positive"
                index_pos = int(
                    np.log(kwargs['filter_dist']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        elif plot_type == "reuse_time_distribution" or plot_type == "rt_distribution":
            xydict, log_base = c_heatmap.\
                hm_reuse_time_distribution(reader.c_reader, time_mode,
                                           time_interval=time_interval,
                                           num_of_pixel_of_time_dim=num_of_pixel_of_time_dim,
                                           num_of_threads=num_of_threads)

            title = "Reuse Time Distribution Heatmap"
            xlabel = '{} Time'.format("Real" if time_mode ==
                                      "r" else "Virtual")
            xticks = ticker.FuncFormatter(
                lambda x, _: '{:.0%}'.format(x / (xydict.shape[1] - 1)))
            ylabel = "Reuse Time"
            yticks = ticker.FuncFormatter(
                lambda x, _: '{:2.0f}'.format(log_base**x - 1))
            imshow_kwargs = {"norm": colors.LogNorm()}

            plot_data = xydict
            np.ma.masked_where(plot_data == 0, plot_data, copy=False)

            if 'filter_rt' in kwargs:
                # make reuse distance < filter_rd invisible,
                # this can be useful when the low-reuse-distance request dominates
                # thus makes the heatmap hard to read
                assert kwargs['filter_rt'] > 0, "filter_rt must be positive"
                index_pos = int(np.log(kwargs['filter_rt']) / np.log(log_base))
                plot_data[:index_pos + 1, :] = 0

            self.draw_heatmap(plot_data,
                              figname=figname,
                              xlabel=xlabel,
                              ylabel=ylabel,
                              xticks=xticks,
                              yticks=yticks,
                              title=title,
                              imshow_kwargs=imshow_kwargs)

        else:
            raise RuntimeError(
                "Cannot recognize this plot type, "
                "please check documentation, yoru input is {}".format(
                    plot_type))

        reader.reset()
Esempio n. 12
0
    def diff_heatmap(self,
                     time_mode,
                     plot_type,
                     algorithm1,
                     time_interval=-1,
                     num_of_pixels=-1,
                     algorithm2="Optimal",
                     cache_params1=None,
                     cache_params2=None,
                     cache_size=-1,
                     **kwargs):
        """
        Plot the differential heatmap between two algorithms by alg2 - alg1

        :param cache_size: size of cache
        :param time_mode: time time_mode "v" for virutal time, "r" for real time
        :param plot_type: same as the name in heatmap function
        :param algorithm1:  name of the first alg
        :param time_interval: same as in heatmap
        :param num_of_pixels: same as in heatmap
        :param algorithm2: name of the second algorithm
        :param cache_params1: parameters of the first algorithm
        :param cache_params2: parameters of the second algorithm
        :param kwargs: include num_of_threads
        """

        figname = kwargs.get("figname", 'differential_heatmap.png')
        num_of_threads = kwargs.get("num_of_threads", DEF_NUM_THREADS)
        assert self.reader is not None, "you haven't opened a trace yet"
        assert cache_size != -1, "you didn't provide size for cache"
        assert cache_size <= self.num_of_req(), \
                    "you cannot specify cache size({}) larger than " \
                    "trace length({})".format(cache_size, self.num_of_req())

        if algorithm1.lower() in C_AVAIL_CACHE and algorithm2.lower(
        ) in C_AVAIL_CACHE:
            hm = CHeatmap()
            hm.diff_heatmap(
                self.reader,
                time_mode,
                plot_type,
                cache_size=cache_size,
                time_interval=time_interval,
                num_of_pixels=num_of_pixels,
                algorithm1=CACHE_NAME_CONVRETER[algorithm1.lower()],
                algorithm2=CACHE_NAME_CONVRETER[algorithm2.lower()],
                cache_params1=cache_params1,
                cache_params2=cache_params2,
                **kwargs)

        else:
            hm = PyHeatmap()
            if algorithm1.lower() not in C_AVAIL_CACHE:
                xydict1 = hm.calculate_heatmap_dat(self.reader,
                                                   time_mode,
                                                   plot_type,
                                                   time_interval=time_interval,
                                                   cache_size=cache_size,
                                                   algorithm=algorithm1,
                                                   cache_params=cache_params1,
                                                   **kwargs)[0]
            else:
                xydict1 = c_heatmap.heatmap(self.reader.c_reader,
                                            time_mode,
                                            plot_type,
                                            cache_size=cache_size,
                                            time_interval=time_interval,
                                            algorithm=algorithm1,
                                            cache_params=cache_params1,
                                            num_of_threads=num_of_threads)

            if algorithm2.lower() not in C_AVAIL_CACHE:
                xydict2 = hm.calculate_heatmap_dat(self.reader,
                                                   time_mode,
                                                   plot_type,
                                                   time_interval=time_interval,
                                                   cache_size=cache_size,
                                                   algorithm=algorithm2,
                                                   cache_params=cache_params2,
                                                   **kwargs)[0]
            else:
                xydict2 = c_heatmap.heatmap(self.reader.c_reader,
                                            time_mode,
                                            plot_type,
                                            time_interval=time_interval,
                                            cache_size=cache_size,
                                            algorithm=algorithm2,
                                            cache_params=cache_params2,
                                            num_of_threads=num_of_threads)

            cHm = CHeatmap()
            text = "      differential heatmap\n      cache size: {},\n      cache type: ({}-{})/{},\n" \
                   "      time type: {},\n      time interval: {},\n      plot type: \n{}".format(
                cache_size, algorithm2, algorithm1, algorithm1, time_mode, time_interval, plot_type)

            x1, y1 = xydict1.shape
            x1 = int(x1 / 2.8)
            y1 /= 8
            if time_mode == 'r':
                time_mode_string = "Real"
            elif time_mode == "v":
                time_mode_string = "Virtual"
            else:
                raise RuntimeError(
                    "unknown time time_mode {}".format(time_mode))

            cHm.set_plot_params(
                'x',
                '{} Time'.format(time_mode_string),
                xydict=xydict1,
                label='Start Time ({})'.format(time_mode_string),
                text=(x1, y1, text))
            cHm.set_plot_params('y',
                                '{} Time'.format(time_mode_string),
                                xydict=xydict1,
                                label='End Time ({})'.format(time_mode_string),
                                fixed_range=(-1, 1))
            np.seterr(divide='ignore', invalid='ignore')

            plot_dict = (xydict2 - xydict1) / xydict1
            cHm.draw_heatmap(plot_dict, figname=figname)
Esempio n. 13
0
    def _prepare_multiprocess_params_LRU(self, mode, plot_type, break_points,
                                         **kwargs):
        # create the array for storing results
        # result is a dict: (x, y) -> heat, x, y is the left, lower point of heat square

        kwargs_subprocess = {
        }  # kw dictionary for passing parameters to subprocess
        kwargs_plot = {}  # kw dictionary passed to plotting functions
        if plot_type == "hit_ratio_start_time_cache_size":
            max_rd = max(kwargs['reuse_dist'])
            kwargs_subprocess['max_rd'] = max_rd
            if 'bin_size' in kwargs:
                kwargs_subprocess["bin_size"] = kwargs['bin_size']
            else:
                kwargs_subprocess['bin_size'] = int(max_rd / DEF_NUM_BIN_PROF)

            result = np.zeros((len(break_points) - 1,
                               max_rd // kwargs_subprocess['bin_size'] + 1),
                              dtype=np.float32)
            result[:] = np.nan
            func_pointer = calc_hit_ratio_start_time_cache_size_subprocess
            kwargs_plot['yticks'] = ticker.FuncFormatter(
                lambda x, pos: '{:2.0f}'.format(kwargs_subprocess['bin_size'] *
                                                x))
            kwargs_plot['xlabel'] = 'time({})'.format(mode)
            kwargs_plot['ylabel'] = 'cache size'
            kwargs_plot['zlabel'] = 'hit rate'
            kwargs_plot['title'] = "hit rate start time cache size"

            # plt.gca().yaxis.set_major_formatter(
            #     ticker.FuncFormatter(lambda x, pos: '{:2.0f}'.format(kwargs_subprocess['bin_size'] * x)))

        elif plot_type == "hit_ratio_start_time_end_time":
            # (x,y) means from time x to time y
            assert 'reader' in kwargs, 'please provide reader as keyword argument'
            assert 'cache_size' in kwargs, 'please provide cache_size as keyword argument'

            reader = kwargs['reader']
            kwargs_subprocess['cache_size'] = kwargs['cache_size']
            array_len = len(break_points) - 1
            result = np.zeros((array_len, array_len), dtype=np.float32)
            # result[:] = np.nan
            func_pointer = calc_hit_ratio_start_time_end_time_subprocess

            kwargs_plot['xlabel'] = 'time({})'.format(mode)
            kwargs_plot['ylabel'] = 'time({})'.format(mode)
            kwargs_plot['yticks'] = ticker.FuncFormatter(
                lambda x, pos: '{:2.0f}%'.format(x * 100 / len(break_points)))
            kwargs_plot['title'] = "hit_ratio_start_time_end_time"

            last_access = c_heatmap.get_last_access_dist(reader.c_reader)
            last_access_array = Array('l', len(last_access), lock=False)
            for i, j in enumerate(last_access):
                last_access_array[i] = j
            kwargs_subprocess['last_access_array'] = last_access_array

        elif plot_type == "avg_rd_start_time_end_time":
            # (x,y) means from time x to time y
            kwargs_plot['xlabel'] = 'time({})'.format(mode)
            kwargs_plot['ylabel'] = 'time({})'.format(mode)
            kwargs_plot['title'] = "avg_rd_start_time_end_time"

            array_len = len(break_points) - 1
            result = np.zeros((array_len, array_len), dtype=np.float32)
            result[:] = np.nan
            func_pointer = calc_avg_rd_start_time_end_time_subprocess

        elif plot_type == "cold_miss_count_start_time_end_time":
            # (x,y) means from time x to time y
            kwargs_plot['xlabel'] = 'time({})'.format(mode)
            kwargs_plot['ylabel'] = 'cold miss count'
            kwargs_plot['title'] = "cold_miss_count_start_time_end_time"
            array_len = len(break_points) - 1
            result = np.zeros((array_len, array_len), dtype=np.float32)
            result[:] = np.nan
            func_pointer = calc_cold_miss_count_start_time_end_time_subprocess

        else:
            raise RuntimeError("cannot recognize plot type")

        kwargs_plot['xticks'] = ticker.FuncFormatter(
            lambda x, pos: '{:2.0f}%'.format(x * 100 / len(break_points)))

        return result, kwargs_subprocess, kwargs_plot, func_pointer