@@ -150,6 +150,20 @@ namespace NLog.LayoutRenderers.Wrappers
             }
         }

+        bool InvalidateCachedValue(LogEventInfo logEvent)
+        {
+            var newCacheKey = CacheKey?.Render(logEvent);
+            if (_cachedValue == null || _renderedCacheKey != newCacheKey || (_cachedValueTimeout.HasValue && logEvent.TimeStamp > _cachedValueExpires))
+            {
+                _renderedCacheKey = newCacheKey;
+                if (_cachedValueTimeout.HasValue)
+                    _cachedValueExpires = logEvent.TimeStamp + _cachedValueTimeout.Value;
+                return true;
+            }
+
+            return false;
+        }
+
         /// <inheritdoc/>
         string IStringValueRenderer.GetFormattedString(LogEventInfo logEvent) => Cached ? RenderInner(logEvent) : null;
     }

//
// Copyright (c) 2004-2019 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
//   contributors may be used to endorse or promote products derived from this
//   software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.LayoutRenderers.Wrappers
{
    using System;
    using System.ComponentModel;
    using NLog.Config;
    using NLog.Internal;
    using NLog.Layouts;

    ///
    /// Applies caching to another layout output.
    ///
    /// <remarks>
    /// The value of the inner layout will be rendered only once and reused subsequently.
    /// </remarks>
    [LayoutRenderer("cached")]
    [AmbientProperty("Cached")]
    [AmbientProperty("ClearCache")]
    [ThreadAgnostic]
    public sealed class CachedLayoutRendererWrapper : WrapperLayoutRendererBase, IStringValueRenderer
    {
        ///
        /// A value indicating when the cache is cleared.
        ///
        [Flags]
        public enum ClearCacheOption
        {
            /// Never clear the cache.
            None = 0,
            /// Clear the cache whenever the <see cref="CachedLayoutRendererWrapper"/> is initialized.
            OnInit = 1,
            /// Clear the cache whenever the <see cref="CachedLayoutRendererWrapper"/> is closed.
            OnClose = 2
        }

        private string _cachedValue;
        private string _renderedCacheKey;

        ///
        /// Initializes a new instance of the <see cref="CachedLayoutRendererWrapper"/> class.
        ///
        public CachedLayoutRendererWrapper()
        {
            Cached = true;
            ClearCache = ClearCacheOption.OnInit | ClearCacheOption.OnClose;
        }

        ///
        /// Gets or sets a value indicating whether this <see cref="CachedLayoutRendererWrapper"/> is enabled.
        ///
        /// <docgen category='Caching Options' order='10' />
        [DefaultValue(true)]
        public bool Cached { get; set; }

        ///
        /// Gets or sets a value indicating when the cache is cleared.
        ///
        /// <docgen category='Caching Options' order='10' />
        public ClearCacheOption ClearCache { get; set; }

        ///
        /// Cachekey. If the cachekey changes, resets the value. For example, the cachekey would be the current day.s
        ///
        /// <docgen category='Caching Options' order='10' />
        public Layout CacheKey { get; set; }

        ///
        /// Initializes the layout renderer.
        ///
        protected override void InitializeLayoutRenderer()
        {
            base.InitializeLayoutRenderer();
            if ((ClearCache & ClearCacheOption.OnInit) == ClearCacheOption.OnInit)
                _cachedValue = null;
        }

        ///
        /// Closes the layout renderer.
        ///
        protected override void CloseLayoutRenderer()
        {
            base.CloseLayoutRenderer();
            if ((ClearCache & ClearCacheOption.OnClose) == ClearCacheOption.OnClose)
                _cachedValue = null;
        }

        ///
        /// Transforms the output of another layout.
        ///
        /// <param name="text">Output to be transform.</param>
        /// <returns>Transformed text.</returns>
        protected override string Transform(string text)
        {
            return text;
        }

        ///
        /// Renders the inner layout contents.
        ///
        /// <param name="logEvent">The log event.</param>
        /// <returns>Contents of inner layout.</returns>
        protected override string RenderInner(LogEventInfo logEvent)
        {
            if (Cached)
            {
                var newCacheKey = CacheKey?.Render(logEvent);
                if (_cachedValue == null || _renderedCacheKey != newCacheKey)
                {
                    _cachedValue = base.RenderInner(logEvent);
                    _renderedCacheKey = newCacheKey;
                }

                return _cachedValue;
            }
            else
            {
                return base.RenderInner(logEvent);
            }
        }

        /// <inheritdoc/>
        string IStringValueRenderer.GetFormattedString(LogEventInfo logEvent) => Cached ? RenderInner(logEvent) : null;
    }
}


@@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
+using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Logging;

 namespace SampleApp

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
using Microsoft.Extensions.Logging;

namespace SampleApp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger("Default");

            app.Run(async context =>
            {
                var connectionFeature = context.Connection;
                logger.LogDebug($"Peer: {connectionFeature.RemoteIpAddress?.ToString()}:{connectionFeature.RemotePort}"
                    + $"{Environment.NewLine}"
                    + $"Sock: {connectionFeature.LocalIpAddress?.ToString()}:{connectionFeature.LocalPort}");

                var response = $"hello, world{Environment.NewLine}";
                context.Response.ContentLength = response.Length;
                context.Response.ContentType = "text/plain";
                await context.Response.WriteAsync(response);
            });
        }

        public static void Main(string[] args)
        {
            TaskScheduler.UnobservedTaskException += (sender, e) =>
            {
                Console.WriteLine("Unobserved exception: {0}", e.Exception);
            };

            var host = new WebHostBuilder()
                .ConfigureLogging((_, factory) =>
                {
                    factory.AddConsole();
                })
                .UseKestrel(options =>
                {
                    // Run callbacks on the transport thread
                    options.ApplicationSchedulingMode = SchedulingMode.Inline;

                    options.Listen(IPAddress.Loopback, 5000, listenOptions =>
                    {
                        // Uncomment the following to enable Nagle's algorithm for this endpoint.
                        //listenOptions.NoDelay = false;

                        listenOptions.UseConnectionLogging();
                    });

                    options.Listen(IPAddress.Loopback, 5001, listenOptions =>
                    {
                        listenOptions.UseHttps("testCert.pfx", "testPassword");
                        listenOptions.UseConnectionLogging();
                    });

                    options.UseSystemd();

                    // The following section should be used to demo sockets
                    //options.ListenUnixSocket("/tmp/kestrel-test.sock");
                })
                .UseLibuv(options =>
                {
                    // Uncomment the following line to change the default number of libuv threads for all endpoints.
                    // options.ThreadCount = 4;
                })
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}


@@ -61,7 +61,7 @@ namespace OpenTelemetry.Trace
             if (activity.IsAllDataRequested)
             {
                 activity.SetResource(this.resource);
-                this.activityProcessor.OnStart(activity);
+                this.activityProcessor?.OnStart(activity);
             }
         }

// <copyright file="ActivitySourceAdapter.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Trace
{
    /// <summary>
    /// This class encapsulates the logic for performing ActivitySource actions
    /// on Activities that are created using default ActivitySource.
    /// All activities created without using ActivitySource will have a
    /// default ActivitySource assigned to them with their name as empty string.
    /// This class is to be used by instrumentation adapters which converts/augments
    /// activies created without ActivitySource, into something which closely
    /// matches the one created using ActivitySource.
    /// </summary>
    /// <remarks>
    /// This class is meant to be only used when writing new Instrumentation for
    /// libraries which are already instrumented with DiagnosticSource/Activity
    /// following this doc:
    /// https://github.com/dotnet/runtime/blob/master/src/libraries/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md.
    /// </remarks>
    public class ActivitySourceAdapter
    {
        private readonly Sampler sampler;
        private readonly Resource resource;
        private ActivityProcessor activityProcessor;

        internal ActivitySourceAdapter(Sampler sampler, ActivityProcessor activityProcessor, Resource resource)
        {
            this.sampler = sampler;
            this.activityProcessor = activityProcessor;
            this.resource = resource;
        }

        private ActivitySourceAdapter()
        {
        }

        /// <summary>
        /// Method that starts an <see cref="Activity"/>.
        /// </summary>
        /// <param name="activity"><see cref="Activity"/> to be started.</param>
        public void Start(Activity activity)
        {
            this.RunGetRequestedData(activity);
            if (activity.IsAllDataRequested)
            {
                activity.SetResource(this.resource);
                this.activityProcessor.OnStart(activity);
            }
        }

        /// <summary>
        /// Method that stops an <see cref="Activity"/>.
        /// </summary>
        /// <param name="activity"><see cref="Activity"/> to be stopped.</param>
        public void Stop(Activity activity)
        {
            if (activity.IsAllDataRequested)
            {
                this.activityProcessor.OnEnd(activity);
            }
        }

        internal void UpdateProcessor(ActivityProcessor processor)
        {
            this.activityProcessor = processor;
        }

        private void RunGetRequestedData(Activity activity)
        {
            ActivityContext parentContext;
            if (string.IsNullOrEmpty(activity.ParentId))
            {
                parentContext = default;
            }
            else if (activity.Parent != null)
            {
                parentContext = activity.Parent.Context;
            }
            else
            {
                parentContext = new ActivityContext(
                    activity.TraceId,
                    activity.ParentSpanId,
                    activity.ActivityTraceFlags,
                    activity.TraceStateString,
                    isRemote: true);
            }

            var samplingParameters = new SamplingParameters(
                parentContext,
                activity.TraceId,
                activity.DisplayName,
                activity.Kind,
                activity.TagObjects,
                activity.Links);

            var samplingResult = this.sampler.ShouldSample(samplingParameters);

            switch (samplingResult.Decision)
            {
                case SamplingDecision.NotRecord:
                    activity.IsAllDataRequested = false;
                    break;
                case SamplingDecision.Record:
                    activity.IsAllDataRequested = true;
                    break;
                case SamplingDecision.RecordAndSampled:
                    activity.IsAllDataRequested = true;
                    activity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;
                    break;
            }
        }
    }
}


@@ -33,7 +33,7 @@ namespace Nethermind.JsonRpc.Modules

         private List<string> _modules = new();
         private List<string> _enabledModules = new();
-
+
         private Dictionary<string, ResolvedMethodInfo> _methods
             = new(StringComparer.InvariantCulture);

//  Copyright (c) 2021 Demerzel Solutions Limited
//  This file is part of the Nethermind library.
//
//  The Nethermind library is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  The Nethermind library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public License
//  along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Nethermind.Logging;
using Newtonsoft.Json;

namespace Nethermind.JsonRpc.Modules
{
    public class RpcModuleProvider : IRpcModuleProvider
    {
        private ILogger _logger;
        private IJsonRpcConfig _jsonRpcConfig;

        private List<string> _modules = new();
        private List<string> _enabledModules = new();

        private Dictionary<string, ResolvedMethodInfo> _methods
            = new(StringComparer.InvariantCulture);

        private readonly Dictionary<string, (Func<bool, Task<IRpcModule>> RentModule, Action<IRpcModule> ReturnModule)> _pools
            = new();

        private IRpcMethodFilter _filter = NullRpcMethodFilter.Instance;

        public RpcModuleProvider(IFileSystem fileSystem, IJsonRpcConfig jsonRpcConfig, ILogManager logManager)
        {
            _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig));
            if (fileSystem.File.Exists(_jsonRpcConfig.CallsFilterFilePath))
            {
                if(_logger.IsWarn) _logger.Warn("Applying JSON RPC filter.");
                _filter = new RpcMethodFilter(_jsonRpcConfig.CallsFilterFilePath, fileSystem, _logger);
            }
        }

        public IReadOnlyCollection<JsonConverter> Converters { get; } = new List<JsonConverter>();

        public IReadOnlyCollection<string> Enabled => _enabledModules;

        public IReadOnlyCollection<string> All => _modules;

        public void Register<T>(IRpcModulePool<T> pool) where T : IRpcModule
        {
            RpcModuleAttribute attribute = typeof(T).GetCustomAttribute<RpcModuleAttribute>();
            if (attribute == null)
            {
                if(_logger.IsWarn) _logger.Warn(
                    $"Cannot register {typeof(T).Name} as a JSON RPC module because it does not have a {nameof(RpcModuleAttribute)} applied.");
                return;
            }

            string moduleType = attribute.ModuleType;

            _pools[moduleType] = (async canBeShared => await pool.GetModule(canBeShared), m => pool.ReturnModule((T) m));
            _modules.Add(moduleType);

            ((List<JsonConverter>) Converters).AddRange(pool.Factory.GetConverters());

            foreach ((string name, (MethodInfo info, bool readOnly, RpcEndpoint availability)) in GetMethodDict(typeof(T)))
            {
                ResolvedMethodInfo resolvedMethodInfo = new(moduleType, info, readOnly, availability);
                if (_filter.AcceptMethod(resolvedMethodInfo.ToString()))
                {
                    _methods[name] = resolvedMethodInfo;
                }
            }

            if (_jsonRpcConfig.EnabledModules.Contains(moduleType, StringComparer.InvariantCultureIgnoreCase))
            {
                _enabledModules.Add(moduleType);
            }
        }

        public ModuleResolution Check(string methodName, RpcEndpoint rpcEndpoint)
        {
            if (!_methods.TryGetValue(methodName, out ResolvedMethodInfo result)) return ModuleResolution.Unknown;

            if ((result.Availability & rpcEndpoint) == RpcEndpoint.None) return ModuleResolution.EndpointDisabled;

            return _enabledModules.Contains(result.ModuleType) ? ModuleResolution.Enabled : ModuleResolution.Disabled;
        }

        public (MethodInfo, bool) Resolve(string methodName)
        {
            if (!_methods.TryGetValue(methodName, out ResolvedMethodInfo result)) return (null, false);

            return (result.MethodInfo, result.ReadOnly);
        }

        public Task<IRpcModule> Rent(string methodName, bool canBeShared)
        {
            if (!_methods.TryGetValue(methodName, out ResolvedMethodInfo result)) return null;

            return _pools[result.ModuleType].RentModule(canBeShared);
        }

        public void Return(string methodName, IRpcModule rpcModule)
        {
            if (!_methods.TryGetValue(methodName, out ResolvedMethodInfo result))
                throw new InvalidOperationException("Not possible to return an unresolved module");

            _pools[result.ModuleType].ReturnModule(rpcModule);
        }

        private IDictionary<string, (MethodInfo, bool, RpcEndpoint)> GetMethodDict(Type type)
        {
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            return methods.ToDictionary(
                x => x.Name.Trim(),
                x =>
                {
                    JsonRpcMethodAttribute? jsonRpcMethodAttribute = x.GetCustomAttribute<JsonRpcMethodAttribute>();
                    return (x, jsonRpcMethodAttribute?.IsSharable ?? true, jsonRpcMethodAttribute?.Availability ?? RpcEndpoint.All);
                });
        }

        private class ResolvedMethodInfo
        {
            public ResolvedMethodInfo(
                string moduleType,
                MethodInfo methodInfo,
                bool readOnly,
                RpcEndpoint availability)
            {
                ModuleType = moduleType;
                MethodInfo = methodInfo;
                ReadOnly = readOnly;
                Availability = availability;
            }

            public string ModuleType { get; }
            public MethodInfo MethodInfo { get; }
            public bool ReadOnly { get; }
            public RpcEndpoint Availability { get; }

            public override string ToString()
            {
                return MethodInfo.Name;
            }
        }
    }
}


@@ -40,12 +40,12 @@ namespace Microsoft.CodeAnalysis.Sarif
                 File.WriteAllText(outputFileName, sarif);
             }

-            if (inputFileName == null) { throw new ArgumentNullException("inputFileName"); };
-            if (outputFileName == null) { throw new ArgumentNullException("outputFileName"); };
+            if (inputFileName == null) { throw new ArgumentNullException(nameof(inputFileName)); };
+            if (outputFileName == null) { throw new ArgumentNullException(nameof(outputFileName)); };

             if (Directory.Exists(outputFileName))
             {
-                throw new ArgumentException("Specified file output path exists but is a directory.", "outputFileName");
+                throw new ArgumentException("Specified file output path exists but is a directory.", nameof(outputFileName));
             }

             if (!conversionOptions.HasFlag(ToolFormatConversionOptions.OverwriteExistingOutputFile) && File.Exists(outputFileName))

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
using Microsoft.CodeAnalysis.Sarif.Converters;
using Microsoft.CodeAnalysis.Sarif.Writers;
using System.Runtime.InteropServices;

namespace Microsoft.CodeAnalysis.Sarif
{
    ///
    /// A class that provides helpers for converting a log file produced by
    /// one of a well-known set of tools to the SARIF format.
    ///
    public class ToolFormatConverter
    {
        /// Converts a tool log file into the SARIF format.
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <exception cref="ArgumentException">Thrown when one or more arguments have unsupported or
        /// illegal values.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception>
        /// <param name="toolFormat">The tool format of the input file.</param>
        /// <param name="inputFileName">The input log file name.</param>
        /// <param name="outputFileName">The name of the file to which the resulting SARIF log shall be
        /// written. This cannot be a directory.</param>
        /// <param name="conversionOptions">Options for controlling the conversion.</param>
        public void ConvertToStandardFormat(
            ToolFormat toolFormat,
            string inputFileName,
            string outputFileName,
            ToolFormatConversionOptions conversionOptions)
        {
            if (toolFormat == ToolFormat.PREfast)
            {
                string sarif = ConvertPREfastToStandardFormat(inputFileName);
                File.WriteAllText(outputFileName, sarif);
            }

            if (inputFileName == null) { throw new ArgumentNullException("inputFileName"); };
            if (outputFileName == null) { throw new ArgumentNullException("outputFileName"); };

            if (Directory.Exists(outputFileName))
            {
                throw new ArgumentException("Specified file output path exists but is a directory.", "outputFileName");
            }

            if (!conversionOptions.HasFlag(ToolFormatConversionOptions.OverwriteExistingOutputFile) && File.Exists(outputFileName))
            {
                throw new InvalidOperationException("Output file already exists and option to overwrite was not specified.");
            }

            // FileMode settings here will results in an exception being raised if the input
            // file does not exist, and that an existing output file will be overwritten
            using (var input = File.OpenRead(inputFileName))
            using (var outputTextStream = File.Create(outputFileName))
            using (var outputTextWriter = new StreamWriter(outputTextStream))
            using (var outputJson = new JsonTextWriter(outputTextWriter))
            {
                if (conversionOptions.HasFlag(ToolFormatConversionOptions.PrettyPrint))
                {
                    outputJson.Formatting = Formatting.Indented;
                }

                using (var output = new ResultLogJsonWriter(outputJson))
                {
                    ConvertToStandardFormat(toolFormat, input, output);
                }
            }
        }

        /// Converts a tool log file to the SARIF format.
        /// <param name="toolFormat">The tool format of the input file.</param>
        /// <param name="inputFileName">The input log file name.</param>
        /// <param name="outputFileName">The name of the file to which the resulting SARIF log shall be
        /// written. This cannot be a directory.</param>
        public void ConvertToStandardFormat(
            ToolFormat toolFormat,
            string inputFileName,
            string outputFileName)
        {
            if (toolFormat == ToolFormat.PREfast)
            {
                string sarif = ConvertPREfastToStandardFormat(inputFileName);
                File.WriteAllText(outputFileName, sarif);
                return;
            }

            ConvertToStandardFormat(toolFormat, inputFileName, outputFileName, ToolFormatConversionOptions.None);
        }

        /// Converts a tool log file represented as a stream into the SARIF format.
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <exception cref="ArgumentException">Thrown when one or more arguments have unsupported or
        /// illegal values.</exception>
        /// <param name="toolFormat">The tool format of the input file.</param>
        /// <param name="inputStream">A stream that contains tool log contents.</param>
        /// <param name="outputStream">A stream to which the converted output should be written.</param>
        public void ConvertToStandardFormat(
            ToolFormat toolFormat,
            Stream inputStream,
            IResultLogWriter outputStream)
        {
            if (toolFormat == ToolFormat.PREfast)
            {
                throw new ArgumentException("Cannot convert PREfast XML from stream. Call ConvertPREfastToStandardFormat helper instead.");
            };

            if (inputStream == null) { throw new ArgumentNullException("inputStream"); };
            if (outputStream == null) { throw new ArgumentNullException("outputStream"); };

            Lazy<IToolFileConverter> converter;
            if (_converters.TryGetValue(toolFormat, out converter))
            {
                converter.Value.Convert(inputStream, outputStream);
            }
            else
            {
                throw new ArgumentException("Unrecognized tool specified: " + toolFormat.ToString(), "toolFormat");
            }
        }

        private readonly IDictionary<ToolFormat, Lazy<IToolFileConverter>> _converters = CreateConverterRecords();

        private static Dictionary<ToolFormat, Lazy<IToolFileConverter>> CreateConverterRecords()
        {
            var result = new Dictionary<ToolFormat, Lazy<IToolFileConverter>>();
            CreateConverterRecord<AndroidStudioConverter>(result, ToolFormat.AndroidStudio);
            CreateConverterRecord<CppCheckConverter>(result, ToolFormat.CppCheck);
            CreateConverterRecord<ClangAnalyzerConverter>(result, ToolFormat.ClangAnalyzer);
            CreateConverterRecord<FortifyConverter>(result, ToolFormat.Fortify);
            CreateConverterRecord<FxCopConverter>(result, ToolFormat.FxCop);
            return result;
        }

        private static void CreateConverterRecord<T>(IDictionary<ToolFormat, Lazy<IToolFileConverter>> dict, ToolFormat format)
            where T : IToolFileConverter, new()
        {
            dict.Add(format, new Lazy<IToolFileConverter>(() => new T(), LazyThreadSafetyMode.ExecutionAndPublication));
        }

        /// Converts a legacy PREfast XML log file into the SARIF format.
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <exception cref="ArgumentException">Thrown when one or more arguments have unsupported or
        /// illegal values.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception>
        /// <param name="toolFormat">The tool format of the input file.</param>
        /// <param name="inputFileName">The input log file name.</param>
        /// <returns>The converted PREfast log file in SARIF format.</returns>
        public static string ConvertPREfastToStandardFormat(string inputFileName)
        {
            if (inputFileName == null) { throw new ArgumentNullException("inputStream"); };

            return ConvertToSarif(inputFileName);
        }

        [return: MarshalAs(UnmanagedType.BStr)]
        [DllImport("PREfastXmlSarifConverter", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        private static extern string ConvertToSarif([MarshalAs(UnmanagedType.BStr)][In]string prefastFilePath);
    }
}


@@ -17,6 +17,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Runtime.CompilerServices;

 namespace OpenTelemetry.Context
 {

// <copyright file="RuntimeContext.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace OpenTelemetry.Context
{
    /// <summary>
    /// Generic runtime context management API.
    /// </summary>
    public sealed class RuntimeContext
    {
        private static readonly ConcurrentDictionary<string, object> Slots = new ConcurrentDictionary<string, object>();

        /// <summary>
        /// Gets or sets the actual context carrier implementation.
        /// </summary>
#if !NET452
        public static Type ContextSlotType { get; set; } = typeof(AsyncLocalRuntimeContextSlot<>);
#else
        public static Type ContextSlotType { get; set; } = typeof(RemotingRuntimeContextSlot<>);
#endif

        /// <summary>
        /// Register a named context slot.
        /// </summary>
        /// <param name="name">The name of the context slot.</param>
        /// <typeparam name="T">The type of the underlying value.</typeparam>
        public static void RegisterSlot<T>(string name)
        {
            lock (Slots)
            {
                if (Slots.ContainsKey(name))
                {
                    throw new InvalidOperationException($"The context slot {name} is already registered.");
                }

                var type = ContextSlotType.MakeGenericType(typeof(T));
                var ctor = type.GetConstructor(new Type[] { typeof(string) });
                Slots[name] = ctor.Invoke(new object[] { name });
            }
        }

        /*
        public static void Apply(IDictionary<string, object> snapshot)
        {
            foreach (var entry in snapshot)
            {
                // TODO: revisit this part if we want Snapshot() to be used on critical paths
                dynamic value = entry.Value;
                SetValue(entry.Key, value);
            }
        }

        public static IDictionary<string, object> Snapshot()
        {
            var retval = new Dictionary<string, object>();
            foreach (var entry in Slots)
            {
                // TODO: revisit this part if we want Snapshot() to be used on critical paths
                dynamic slot = entry.Value;
                retval[entry.Key] = slot.Get();
            }
            return retval;
        }
        */

        /// <summary>
        /// Sets the value to a registered slot.
        /// </summary>
        /// <param name="name">The name of the context slot.</param>
        /// <param name="value">The value to be set.</param>
        /// <typeparam name="T">The type of the value.</typeparam>
        public static void SetValue<T>(string name, T value)
        {
            var slot = (RuntimeContextSlot<T>)Slots[name];
            slot.Set(value);
        }

        /// <summary>
        /// Gets the value from a registered slot.
        /// </summary>
        /// <param name="name">The name of the context slot.</param>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <returns>The value retrieved from the context slot.</returns>
        public static T GetValue<T>(string name)
        {
            var slot = (RuntimeContextSlot<T>)Slots[name];
            return slot.Get();
        }

        // For testing purpose
        // private static Clear
    }
}


@@ -47,13 +47,23 @@ def copy_files(use_gpu=False):

 def clear_path(path):
-    contents = os.listdir(path)
-    for file in contents:
-        file_path = os.path.join(path, file)
-        if os.path.isfile(file_path):
-            os.remove(file_path)
-        else:
-            shutil.rmtree(file_path)
+    if os.path.isdir(path):
+        contents = os.listdir(path)
+        for file in contents:
+            file_path = os.path.join(path, file)
+            if os.path.isfile(file_path):
+                os.remove(file_path)
+            else:
+                shutil.rmtree(file_path)
+
+
+def silent_call(cmd):
+    try:
+        with open(os.devnull, "w") as shut_up:
+            subprocess.check_output(cmd, stderr=shut_up)
+            return 0
+    except Exception:
+        return 1

 def compile_cpp(use_mingw=False, use_gpu=False):

# coding: utf-8
# pylint: disable=invalid-name, exec-used, C0111
"""Setup lightgbm package."""
from __future__ import absolute_import

import distutils
import os
import shutil
import struct
import sys

from setuptools import find_packages, setup
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.sdist import sdist

def find_lib():
    CURRENT_DIR = os.path.dirname(__file__)
    libpath_py = os.path.join(CURRENT_DIR, 'lightgbm/libpath.py')
    libpath = {'__file__': libpath_py}
    exec(compile(open(libpath_py, "rb").read(), libpath_py, 'exec'), libpath, libpath)

    LIB_PATH = [os.path.relpath(path, CURRENT_DIR) for path in libpath['find_lib_path']()]
    print("Install lib_lightgbm from: %s" % LIB_PATH)
    return LIB_PATH

def copy_files(use_gpu=False):

    def copy_files_helper(folder_name):
        src = os.path.join('..', folder_name)
        if os.path.exists(src):
            dst = os.path.join('./lightgbm', folder_name)
            shutil.rmtree(dst, ignore_errors=True)
            distutils.dir_util.copy_tree(src, dst)
        else:
            raise Exception('Cannot copy {} folder'.format(src))

    if not os.path.isfile('./_IS_SOURCE_PACKAGE.txt'):
        copy_files_helper('include')
        copy_files_helper('src')
        if use_gpu:
            copy_files_helper('compute')
        distutils.file_util.copy_file("../CMakeLists.txt", "./lightgbm/")
        distutils.file_util.copy_file("../LICENSE", "./")

def clear_path(path):
    contents = os.listdir(path)
    for file in contents:
        file_path = os.path.join(path, file)
        if os.path.isfile(file_path):
            os.remove(file_path)
        else:
            shutil.rmtree(file_path)

def compile_cpp(use_mingw=False, use_gpu=False):

    if os.path.exists("build_cpp"):
        shutil.rmtree("build_cpp")
    os.makedirs("build_cpp")
    os.chdir("build_cpp")

    cmake_cmd = "cmake "
    build_cmd = "make _lightgbm"
    if use_gpu:
        cmake_cmd += " -DUSE_GPU=ON "
    if os.name == "nt":
        if use_mingw:
            cmake_cmd += " -G \"MinGW Makefiles\" "
            os.system(cmake_cmd + " ../lightgbm/")
            build_cmd = "mingw32-make.exe _lightgbm"
        else:
            vs_versions = ["Visual Studio 15 2017 Win64", "Visual Studio 14 2015 Win64", "Visual Studio 12 2013 Win64"]
            try_vs = 1
            for vs in vs_versions:
                tmp_cmake_cmd = "%s -G \"%s\"" % (cmake_cmd, vs)
                try_vs = os.system(tmp_cmake_cmd + " ../lightgbm/")
                if try_vs == 0:
                    cmake_cmd = tmp_cmake_cmd
                    break
                else:
                    clear_path("./")
            if try_vs != 0:
                raise Exception('Please install Visual Studio or MS Build first')

            build_cmd = "cmake --build . --target _lightgbm  --config Release"
    print("Start to compile library.")
    os.system(cmake_cmd + " ../lightgbm/")
    os.system(build_cmd)
    os.chdir("..")

class CustomInstallLib(install_lib):

    def install(self):
        outfiles = install_lib.install(self)
        src = find_lib()[0]
        dst = os.path.join(self.install_dir, 'lightgbm')
        dst, _ = self.copy_file(src, dst)
        outfiles.append(dst)
        return outfiles

class CustomInstall(install):

    user_options = install.user_options + [
        ('mingw', 'm', 'compile with mingw'),
        ('gpu', 'g', 'compile gpu version'),
        ('precompile', 'p', 'use precompile library')
    ]

    def initialize_options(self):
        install.initialize_options(self)
        self.mingw = 0
        self.gpu = 0
        self.precompile = 0

    def run(self):
        if not self.precompile:
            copy_files(use_gpu=self.gpu)
            compile_cpp(use_mingw=self.mingw, use_gpu=self.gpu)
        self.distribution.data_files = [('lightgbm', find_lib())]
        install.run(self)

class CustomSdist(sdist):

    def run(self):
        copy_files(use_gpu=True)
        open("./_IS_SOURCE_PACKAGE.txt", 'w').close()
        if os.path.exists("./lightgbm/Release/"):
            shutil.rmtree('./lightgbm/Release/')
        if os.path.isfile('./lightgbm/lib_lightgbm.so'):
            os.remove('./lightgbm/lib_lightgbm.so')
        sdist.run(self)
        if os.path.isfile('./_IS_SOURCE_PACKAGE.txt'):
            os.remove('./_IS_SOURCE_PACKAGE.txt')

if __name__ == "__main__":
    if (8 * struct.calcsize("P")) != 64:
        raise Exception('Cannot install LightGBM in 32-bit python, please use 64-bit python instead.')

    dir_path = os.path.dirname(os.path.realpath(__file__))
    if os.path.isfile(os.path.join('..', 'VERSION.txt')):
        distutils.file_util.copy_file(
            os.path.join('..', 'VERSION.txt'),
            os.path.join('.', 'lightgbm'))
    if os.path.isfile(os.path.join(dir_path, 'lightgbm', 'VERSION.txt')):
        version = open(os.path.join(dir_path, 'lightgbm', 'VERSION.txt')).read().strip()

    sys.path.insert(0, '.')

    setup(name='lightgbm',
          version=version,
          description='LightGBM Python Package',
          long_description=open('README.rst').read(),
          install_requires=[
              'numpy',
              'scipy',
              'scikit-learn'
          ],
          maintainer='Guolin Ke',
          maintainer_email='guolin.ke@microsoft.com',
          zip_safe=False,
          cmdclass={
              'install': CustomInstall,
              'install_lib': CustomInstallLib,
              'sdist': CustomSdist,
          },
          packages=find_packages(),
          include_package_data=True,
          data_files=[],
          license='The MIT License (Microsoft)',
          url='https://github.com/Microsoft/LightGBM')


@@ -48,7 +48,7 @@ static PJ_LP fouc_s_s_inverse (PJ_XY xy, PJ *P) {           /* Spheroidal, inver
             lp.phi = xy.y < 0. ? -M_HALFPI : M_HALFPI;
     } else
         lp.phi = aasin(P->ctx,xy.y);
-    V = cos(lp.phi);
+    const double V = cos(lp.phi);
     lp.lam = xy.x * (Q->n + Q->n1 * V) / V;
     return lp;
 }

#define PJ_LIB__

#include <errno.h>
#include <math.h>

#include "proj.h"
#include "proj_internal.h"

PROJ_HEAD(fouc_s, "Foucaut Sinusoidal") "\n\tPCyl, Sph";

#define MAX_ITER    10
#define LOOP_TOL    1e-7

namespace { // anonymous namespace
struct pj_opaque {
    double n, n1;
};
} // anonymous namespace

static PJ_XY fouc_s_s_forward (PJ_LP lp, PJ *P) {           /* Spheroidal, forward */
    PJ_XY xy = {0.0,0.0};
    struct pj_opaque *Q = static_cast<struct pj_opaque*>(P->opaque);
    double t;

    t = cos(lp.phi);
    xy.x = lp.lam * t / (Q->n + Q->n1 * t);
    xy.y = Q->n * lp.phi + Q->n1 * sin(lp.phi);
    return xy;
}

static PJ_LP fouc_s_s_inverse (PJ_XY xy, PJ *P) {           /* Spheroidal, inverse */
    PJ_LP lp = {0.0,0.0};
    struct pj_opaque *Q = static_cast<struct pj_opaque*>(P->opaque);
    double V;
    int i;

    if (Q->n != 0.0) {
        lp.phi = xy.y;
        for (i = MAX_ITER; i ; --i) {
            lp.phi -= V = (Q->n * lp.phi + Q->n1 * sin(lp.phi) - xy.y ) /
                (Q->n + Q->n1 * cos(lp.phi));
            if (fabs(V) < LOOP_TOL)
                break;
        }
        if (!i)
            lp.phi = xy.y < 0. ? -M_HALFPI : M_HALFPI;
    } else
        lp.phi = aasin(P->ctx,xy.y);
    V = cos(lp.phi);
    lp.lam = xy.x * (Q->n + Q->n1 * V) / V;
    return lp;
}

PJ *PROJECTION(fouc_s) {
    struct pj_opaque *Q = static_cast<struct pj_opaque*>(pj_calloc (1, sizeof (struct pj_opaque)));
    if (nullptr==Q)
        return pj_default_destructor (P, ENOMEM);
    P->opaque = Q;

    Q->n = pj_param(P->ctx, P->params, "dn").f;
    if (Q->n < 0. || Q->n > 1.)
        return pj_default_destructor (P, PJD_ERR_N_OUT_OF_RANGE);

    Q->n1 = 1. - Q->n;
    P->es = 0;
    P->inv = fouc_s_s_inverse;
    P->fwd = fouc_s_s_forward;
    return P;
}


@@ -49,7 +49,7 @@ void DataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
   int crop_size = this->layer_param_.transform_param().crop_size();
   if (crop_size > 0) {
     top[0]->Reshape(this->layer_param_.data_param().batch_size(),
-                       datum.channels(), crop_size, crop_size);
+        datum.channels(), crop_size, crop_size);
     this->prefetch_data_.Reshape(this->layer_param_.data_param().batch_size(),
         datum.channels(), crop_size, crop_size);
     this->transformed_data_.Reshape(1, datum.channels(), crop_size, crop_size);

#include <opencv2/core/core.hpp>

#include <stdint.h>

#include <string>
#include <vector>

#include "caffe/common.hpp"
#include "caffe/data_layers.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/benchmark.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/util/rng.hpp"

namespace caffe {

template <typename Dtype>
DataLayer<Dtype>::~DataLayer<Dtype>() {
  this->JoinPrefetchThread();
}

template <typename Dtype>
void DataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  // Initialize DB
  db_.reset(db::GetDB(this->layer_param_.data_param().backend()));
  db_->Open(this->layer_param_.data_param().source(), db::READ);
  cursor_.reset(db_->NewCursor());

  // Check if we should randomly skip a few data points
  if (this->layer_param_.data_param().rand_skip()) {
    unsigned int skip = caffe_rng_rand() %
                        this->layer_param_.data_param().rand_skip();
    LOG(INFO) << "Skipping first " << skip << " data points.";
    while (skip-- > 0) {
      cursor_->Next();
    }
  }
  // Read a data point, and use it to initialize the top blob.
  Datum datum;
  datum.ParseFromString(cursor_->value());

  if (DecodeDatum(&datum)) {
    LOG(INFO) << "Decoding Datum";
  }
  // image
  int crop_size = this->layer_param_.transform_param().crop_size();
  if (crop_size > 0) {
    top[0]->Reshape(this->layer_param_.data_param().batch_size(),
                       datum.channels(), crop_size, crop_size);
    this->prefetch_data_.Reshape(this->layer_param_.data_param().batch_size(),
        datum.channels(), crop_size, crop_size);
    this->transformed_data_.Reshape(1, datum.channels(), crop_size, crop_size);
  } else {
    top[0]->Reshape(
        this->layer_param_.data_param().batch_size(), datum.channels(),
        datum.height(), datum.width());
    this->prefetch_data_.Reshape(this->layer_param_.data_param().batch_size(),
        datum.channels(), datum.height(), datum.width());
    this->transformed_data_.Reshape(1, datum.channels(),
      datum.height(), datum.width());
  }
  LOG(INFO) << "output data size: " << top[0]->num() << ","
      << top[0]->channels() << "," << top[0]->height() << ","
      << top[0]->width();
  // label
  if (this->output_labels_) {
    top[1]->Reshape(this->layer_param_.data_param().batch_size(), 1, 1, 1);
    this->prefetch_label_.Reshape(this->layer_param_.data_param().batch_size(),
        1, 1, 1);
  }
}

// This function is used to create a thread that prefetches the data.
template <typename Dtype>
void DataLayer<Dtype>::InternalThreadEntry() {
  CPUTimer batch_timer;
  batch_timer.Start();
  double read_time = 0;
  double trans_time = 0;
  CPUTimer timer;
  CHECK(this->prefetch_data_.count());
  CHECK(this->transformed_data_.count());
  Dtype* top_data = this->prefetch_data_.mutable_cpu_data();
  Dtype* top_label = NULL;  // suppress warnings about uninitialized variables

  if (this->output_labels_) {
    top_label = this->prefetch_label_.mutable_cpu_data();
  }
  const int batch_size = this->layer_param_.data_param().batch_size();
  for (int item_id = 0; item_id < batch_size; ++item_id) {
    timer.Start();
    // get a blob
    Datum datum;
    datum.ParseFromString(cursor_->value());

    cv::Mat cv_img;
    if (datum.encoded()) {
       cv_img = DecodeDatumToCVMat(datum);
    }
    read_time += timer.MicroSeconds();
    timer.Start();

    // Apply data transformations (mirror, scale, crop...)
    int offset = this->prefetch_data_.offset(item_id);
    this->transformed_data_.set_cpu_data(top_data + offset);
    if (datum.encoded()) {
      this->data_transformer_.Transform(cv_img, &(this->transformed_data_));
    } else {
      this->data_transformer_.Transform(datum, &(this->transformed_data_));
    }
    if (this->output_labels_) {
      top_label[item_id] = datum.label();
    }
    trans_time += timer.MicroSeconds();
    // go to the next iter
    cursor_->Next();
    if (!cursor_->valid()) {
      DLOG(INFO) << "Restarting data prefetching from start.";
      cursor_->SeekToFirst();
    }
  }
  batch_timer.Stop();
  DLOG(INFO) << "Prefetch batch: " << batch_timer.MilliSeconds() << " ms.";
  DLOG(INFO) << "     Read time: " << read_time / 1000 << " ms.";
  DLOG(INFO) << "Transform time: " << trans_time / 1000 << " ms.";
}

INSTANTIATE_CLASS(DataLayer);
REGISTER_LAYER_CLASS(Data);

}  // namespace caffe


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Graph
{
    int numberVertex;
    vector <int> *adjacency;

  public:
    // Constructor to initialise graph
    Graph(int numberVertex)
    {
        this->numberVertex = numberVertex;
        adjacency = new vector <int> [numberVertex];
    }

    // Function to add edge between source and destination
    void addEdge(int source, int destination)
    {
        adjacency[source].push_back(destination);
    }

    // Function to perform Breadth First Search
    void bfs(int starting);
};

void Graph::bfs(int starting)
{
    bool visited[numberVertex];

    for (int i = 0; i < numberVertex; i++)
        visited[i] = false;

    queue <int> queue_vertex;

    visited[starting] = true;
    queue_vertex.push(starting);

    while (!queue_vertex.empty())
    {
        starting = queue_vertex.front();
        cout << starting << " ";
        queue_vertex.pop();

        for (vector <int> :: iterator it = adjacency[starting].begin(); it != adjacency[starting].end(); ++it)
        {
            if(!visited[*it])
            {
                visited[*it] = true;
                queue_vertex.push(*it);
            }
        }
    }
}

int main()
{
    // Number of vertices is 8
    Graph graph(8);

    // Create edges between vertices
    graph.addEdge(0, 1);
    graph.addEdge(0, 2);
    graph.addEdge(1, 2);
    graph.addEdge(1, 4);
    graph.addEdge(2, 0);
    graph.addEdge(2, 3);
    graph.addEdge(3, 3);
    graph.addEdge(3, 6);
    graph.addEdge(4, 0);
    graph.addEdge(4, 5);
    graph.addEdge(5, 6);
    graph.addEdge(5, 7);
    graph.addEdge(6, 2);
    graph.addEdge(7, 3);

    cout << "Breadth First Traversal is : ";
    graph.bfs(0);

    return 0;
}

/* Output

Breadth First Traversal is : 0 1 2 4 3 5 6 7

*/


@@ -1,6 +1,7 @@
 #include <cstdint>

 #include <iostream>
+#include <numeric>
 #include <stdexcept>

 #include <adios2.h>

#include <cstdint>

#include <iostream>
#include <stdexcept>

#include <adios2.h>

#include <gtest/gtest.h>

#ifdef ADIOS2_HAVE_MPI

TEST(ADIOSInterface, MPICommRemoved)
{
    MPI_Comm myComm;
    MPI_Comm_dup(MPI_COMM_WORLD, &myComm);
    adios2::ADIOS adios(myComm);
    adios2::IO io = adios.DeclareIO("TestIO");
    MPI_Comm_free(&myComm);

    adios2::Engine engine = io.Open("test.bp", adios2::Mode::Write);
}

#endif

class ADIOS2_CXX11_API : public ::testing::Test
{
public:
    ADIOS2_CXX11_API()
#ifdef ADIOS2_HAVE_MPI
    : ad(MPI_COMM_WORLD, adios2::DebugON)
#else
    : ad(adios2::DebugON)
#endif
    {
#ifdef ADIOS2_HAVE_MPI
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
#endif
    }

    adios2::ADIOS ad;
    int rank = 0;
    int size = 1;
};

class ADIOS2_CXX11_API_IO : public ADIOS2_CXX11_API
{
public:
    ADIOS2_CXX11_API_IO() : io(ad.DeclareIO("CXX11_API_TestIO")) {}

    adios2::IO io;
};

TEST_F(ADIOS2_CXX11_API_IO, Engine)
{
    io.SetEngine("bpfile");
    EXPECT_EQ(io.EngineType(), "bpfile");

    adios2::Engine engine = io.Open("types.bp", adios2::Mode::Write);
    EXPECT_EQ(engine.Name(), "types.bp");
    EXPECT_EQ(engine.Type(), "BP3");

    EXPECT_EQ(io.EngineType(), "bp"); // FIXME? Is it expected that adios2_open
                                      // changes the engine_type string?
}

TEST_F(ADIOS2_CXX11_API_IO, EngineDefault)
{
    io.SetEngine("");
    EXPECT_EQ(io.EngineType(), "");

    adios2::Engine engine = io.Open("types.bp", adios2::Mode::Write);
    EXPECT_EQ(engine.Name(), "types.bp");
    EXPECT_EQ(engine.Type(), "BP3");

    EXPECT_EQ(io.EngineType(), "bp"); // FIXME? Is it expected that adios2_open
                                      // changes the engine_type string?
}

int main(int argc, char **argv)
{
#ifdef ADIOS2_HAVE_MPI
    MPI_Init(nullptr, nullptr);
#endif

    int result;
    ::testing::InitGoogleTest(&argc, argv);
    result = RUN_ALL_TESTS();

#ifdef ADIOS2_HAVE_MPI
    MPI_Finalize();
#endif

    return result;
}


@@ -29,6 +29,7 @@ THE SOFTWARE.
 #include <thread>
 #include <future>
 #include <functional>
+#include <vector>

 #define NUM_GROUPS 1
 #define GROUP_SIZE 1

/*
Copyright (c) 2020-present Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdio.h>
#include "hip/hip_runtime.h"
#ifdef __HIP_PLATFORM_HCC__
#include "hip/hip_ext.h"
#endif
#include <iostream>
#include <chrono>
#include <algorithm>
#include <atomic>
#include <thread>
#include <future>
#include <functional>

#define NUM_GROUPS 1
#define GROUP_SIZE 1
#define WARMUP_RUN_COUNT 10
#define TIMING_RUN_COUNT 100
#define TOTAL_RUN_COUNT WARMUP_RUN_COUNT + TIMING_RUN_COUNT

__global__ void EmptyKernel() {}

// Helper to print various timing metrics
void print_timing(std::string test, std::array<float, TOTAL_RUN_COUNT> &results, int batch = 1)
{

    float total_us = 0.0f, mean_us = 0.0f, stddev_us = 0.0f;

    // remove top outliers due to nature of variability across large number of multi-threaded runs
    std::sort(results.begin(), results.end(), std::greater<float>());
    auto start_iter = std::next(results.begin(), WARMUP_RUN_COUNT);
    auto end_iter = results.end();

    // mean
    std::for_each(start_iter, end_iter, [&](const float &run_ms) {
        total_us += (run_ms * 1000) / batch;
    });
    mean_us = total_us  / TIMING_RUN_COUNT;

   // stddev
    total_us = 0;
    std::for_each(start_iter, end_iter, [&](const float &run_ms) {
        float dev_us = ((run_ms * 1000) / batch) - mean_us;
        total_us += dev_us * dev_us;
    });
    stddev_us = sqrt(total_us / TIMING_RUN_COUNT);

    printf("\n %s: %.1f us, std: %.1f us\n", test.c_str(), mean_us, stddev_us);
}

// Measure time taken to enqueue a kernel on the GPU using hipModuleLaunchKernel
void hipModuleLaunchKernel_enqueue_rate(std::atomic_int* shared, int max_threads)
{
    //resources necessary for this thread
    hipStream_t stream;
    hipStreamCreate(&stream);
    hipModule_t module;
    hipFunction_t function;
    hipModuleLoad(&module, "test_kernel.code");
    hipModuleGetFunction(&function, module, "test");
    void* kernel_params = nullptr;
    std::array<float, TOTAL_RUN_COUNT> results;

    //synchronize all threads, before running
    int tid = shared->fetch_add(1, std::memory_order_release);
    while (max_threads != shared->load(std::memory_order_acquire)) {}

    for (auto i = 0; i < TOTAL_RUN_COUNT; ++i) {
        auto start = std::chrono::high_resolution_clock::now();
        hipModuleLaunchKernel(function, 1, 1, 1, 1, 1, 1, 0, stream, &kernel_params, nullptr);
        auto stop = std::chrono::high_resolution_clock::now();
        results[i] = std::chrono::duration<double, std::milli>(stop - start).count();
    }
    print_timing("Thread ID : " + std::to_string(tid) + " , " + "hipModuleLaunchKernel enqueue rate", results);
}

// Measure time taken to enqueue a kernel on the GPU using hipLaunchKernelGGL
void hipLaunchKernelGGL_enqueue_rate(std::atomic_int* shared, int max_threads)
{
    //resources necessary for this thread
    hipStream_t stream;
    hipStreamCreate(&stream);
    std::array<float, TOTAL_RUN_COUNT> results;

    //synchronize all threads, before running
    int tid = shared->fetch_add(1, std::memory_order_release);
    while (max_threads != shared->load(std::memory_order_acquire)) {}

    for (auto i = 0; i < TOTAL_RUN_COUNT; ++i) {
        auto start = std::chrono::high_resolution_clock::now();
        hipLaunchKernelGGL((EmptyKernel), dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream);
        auto stop = std::chrono::high_resolution_clock::now();
        results[i] = std::chrono::duration<double, std::milli>(stop - start).count();
    }
    print_timing("Thread ID : " + std::to_string(tid) + " , " + "hipLaunchKernelGGL enqueue rate", results);
}

// Simple thread pool
struct thread_pool {
    thread_pool(int total_threads) : max_threads(total_threads) {}
    void start(std::function<void(std::atomic_int*, int)> f) {
        for (int i = 0; i < max_threads; ++i) {
            threads.push_back(std::async(std::launch::async, f, &shared, max_threads));
        }
    }
    void finish() {
        for (auto&&thread : threads) {
            thread.get();
        }
        threads.clear();
        shared = {0};
    }
    ~thread_pool() {
        finish();
    }
private:
    std::atomic_int shared {0};
    std::vector<std::future<void>> threads;
    int max_threads = 1;
};

int main(int argc, char* argv[])
{
    if (argc != 3) {
        std::cerr << "Run test as 'hipDispatchEnqueueRateMT <num_threads> <0-hipModuleLaunchKernel /1-hipLaunchKernelGGL>'\n";
        return -1;
    }

    int max_threads = atoi(argv[1]);
    int run_module_test = atoi(argv[2]);
    if(max_threads < 1 || run_module_test < 0 || run_module_test > 1) {
        std::cerr << "Invalid Input.\n";
        std::cerr << "Run test as 'hipDispatchEnqueueRateMT <num_threads> <0-hipModuleLaunchKernel /1-hipLaunchKernelGGL>'\n";
        return -1;
    }
    thread_pool task(max_threads);

    if(run_module_test == 0) {
        task.start(hipModuleLaunchKernel_enqueue_rate);
        task.finish();
    } else {
        task.start(hipLaunchKernelGGL_enqueue_rate);
        task.finish();
    }

    return 0;
}


@@ -95,4 +95,4 @@ PeriodicTable *PeriodicTable::getTable() {
   return ds_instance.get();
 }

-}  // end of namespace
+}  // namespace RDKit

// $Id$
//
//  Copyright (C) 2001-2006 Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include "PeriodicTable.h"
#include <string>
#include <boost/tokenizer.hpp>
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
#include <sstream>
#include <locale>

#if RDK_BUILD_THREADSAFE_SSS
#include <mutexp>
#endif

namespace RDKit {

class std::unique_ptr<PeriodicTable> PeriodicTable::ds_instance = nullptr;

PeriodicTable::PeriodicTable() {
  // it is assumed that the atomic atomData string constains atoms
  // in sequence and no atoms are missing in between
  byanum.clear();
  byname.clear();

  boost::char_separator<char> eolSep("\n");
  tokenizer tokens(periodicTableAtomData, eolSep);
  for (tokenizer::iterator token = tokens.begin(); token != tokens.end();
       ++token) {
    if (*token != " ") {
      atomicData adata(*token);
      byanum.push_back(adata);
      std::string enam = adata.Symbol();
      byname[enam] = adata.AtomicNum();
    }
  }

  unsigned int lidx = 0;
  std::istringstream istr;
  istr.imbue(std::locale("C"));
  while (isotopesAtomData[lidx] != "" && isotopesAtomData[lidx] != "EOS") {
    tokenizer lines(isotopesAtomData[lidx++], eolSep);
    boost::char_separator<char> spaceSep(" \t");
    for (tokenizer::iterator line = lines.begin(); line != lines.end();
         ++line) {
      if (*line != " ") {
        tokenizer tokens(*line, spaceSep);
        tokenizer::iterator token = tokens.begin();
        int anum;
        istr.clear();
        istr.str(*token);
        istr >> anum;
        atomicData &adata = byanum[anum];
        ++token;
        if (token == tokens.end()) continue;
        ++token;
        if (token == tokens.end()) continue;
        unsigned int isotope;
        istr.clear();
        istr.str(*token);
        istr >> isotope;
        ++token;
        if (token == tokens.end()) continue;
        double mass;
        istr.clear();
        istr.str(*token);
        istr >> mass;
        ++token;
        if (token == tokens.end()) continue;
        double abundance;
        istr.clear();
        istr.str(*token);
        istr >> abundance;
        adata.d_isotopeInfoMap[isotope] = std::make_pair(mass, abundance);
      }
    }
  }
}

void PeriodicTable::initInstance() { ds_instance = std::unique_ptr<PeriodicTable>(new PeriodicTable()); }

PeriodicTable *PeriodicTable::getTable() {
#if RDK_BUILD_THREADSAFE_SSS
  static std::once_flag pt_init_once;
  std::call_once(pt_init_once, initInstance);
#else
  if (!ds_instance) initInstance();
#endif
  return ds_instance.get();
}

}  // end of namespace


@@ -13,30 +13,35 @@ SyncedMemory::~SyncedMemory() {
   if (cpu_ptr_ && own_cpu_data_) {
     CaffeFreeHost(cpu_ptr_);
   }
-
-  if (gpu_ptr_) {
-    CUDA_CHECK(cudaFree(gpu_ptr_));
+  if (gpu_data_) {
+    CUDA_CHECK(cudaFree(gpu_data_));
   }
 }

 inline void SyncedMemory::to_cpu() {
   switch (head_) {
   case UNINITIALIZED:
-    CaffeMallocHost(&cpu_ptr_, size_);
-    memset(cpu_ptr_, 0, size_);
+    cpu_resize();
     head_ = HEAD_AT_CPU;
     own_cpu_data_ = true;
     break;
   case HEAD_AT_GPU:
+    gpu_resize();
+    cpu_resize();
+    CUDA_CHECK(cudaMemcpy(cpu_data_, gpu_data_, size_, cudaMemcpyDeviceToHost));
     if (cpu_ptr_ == NULL) {
       CaffeMallocHost(&cpu_ptr_, size_);
       own_cpu_data_ = true;
     }
-    CUDA_CHECK(cudaMemcpy(cpu_ptr_, gpu_ptr_, size_, cudaMemcpyDeviceToHost));
     head_ = SYNCED;
     break;
   case HEAD_AT_CPU:
+    cpu_resize();
+    break;
   case SYNCED:
+    if (cpu_resize()) {
+      head_ = HEAD_AT_CPU;
+    }
     break;
   }
 }

// Copyright 2014 BVLC and contributors.

#include <cuda_runtime.h>

#include <cstring>

#include "caffe/common.hpp"
#include "caffe/syncedmem.hpp"

namespace caffe {

SyncedMemory::~SyncedMemory() {
  if (cpu_ptr_ && own_cpu_data_) {
    CaffeFreeHost(cpu_ptr_);
  }

  if (gpu_ptr_) {
    CUDA_CHECK(cudaFree(gpu_ptr_));
  }
}

inline void SyncedMemory::to_cpu() {
  switch (head_) {
  case UNINITIALIZED:
    CaffeMallocHost(&cpu_ptr_, size_);
    memset(cpu_ptr_, 0, size_);
    head_ = HEAD_AT_CPU;
    own_cpu_data_ = true;
    break;
  case HEAD_AT_GPU:
    if (cpu_ptr_ == NULL) {
      CaffeMallocHost(&cpu_ptr_, size_);
      own_cpu_data_ = true;
    }
    CUDA_CHECK(cudaMemcpy(cpu_ptr_, gpu_ptr_, size_, cudaMemcpyDeviceToHost));
    head_ = SYNCED;
    break;
  case HEAD_AT_CPU:
  case SYNCED:
    break;
  }
}

inline void SyncedMemory::to_gpu() {
  switch (head_) {
  case UNINITIALIZED:
    CUDA_CHECK(cudaMalloc(&gpu_ptr_, size_));
    CUDA_CHECK(cudaMemset(gpu_ptr_, 0, size_));
    head_ = HEAD_AT_GPU;
    break;
  case HEAD_AT_CPU:
    if (gpu_ptr_ == NULL) {
      CUDA_CHECK(cudaMalloc(&gpu_ptr_, size_));
    }
    CUDA_CHECK(cudaMemcpy(gpu_ptr_, cpu_ptr_, size_, cudaMemcpyHostToDevice));
    head_ = SYNCED;
    break;
  case HEAD_AT_GPU:
  case SYNCED:
    break;
  }
}

const void* SyncedMemory::cpu_data() {
  to_cpu();
  return (const void*)cpu_ptr_;
}

void SyncedMemory::set_cpu_data(void* data) {
  CHECK(data);
  if (own_cpu_data_) {
    CaffeFreeHost(cpu_ptr_);
  }
  cpu_ptr_ = data;
  head_ = HEAD_AT_CPU;
  own_cpu_data_ = false;
}

const void* SyncedMemory::gpu_data() {
  to_gpu();
  return (const void*)gpu_ptr_;
}

void* SyncedMemory::mutable_cpu_data() {
  to_cpu();
  head_ = HEAD_AT_CPU;
  return cpu_ptr_;
}

void* SyncedMemory::mutable_gpu_data() {
  to_gpu();
  head_ = HEAD_AT_GPU;
  return gpu_ptr_;
}

}  // namespace caffe


@@ -22,6 +22,7 @@
 #include "protocol.h"
 #include "outputmessage.h"
 #include "rsa.h"
+#include "xtea.h"

 extern RSA g_RSA;

/**
 * The Forgotten Server - a free and open-source MMORPG server emulator
 * Copyright (C) 2018  Mark Samman <mark.samman@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "otpch.h"

#include "protocol.h"
#include "outputmessage.h"
#include "rsa.h"

extern RSA g_RSA;

void Protocol::onSendMessage(const OutputMessage_ptr& msg) const
{
	if (!rawMessages) {
		msg->writeMessageLength();

		if (encryptionEnabled) {
			XTEA_encrypt(*msg);
			msg->addCryptoHeader(checksumEnabled);
		}
	}
}

void Protocol::onRecvMessage(NetworkMessage& msg)
{
	if (encryptionEnabled && !XTEA_decrypt(msg)) {
		return;
	}

	parsePacket(msg);
}

OutputMessage_ptr Protocol::getOutputBuffer(int32_t size)
{
	//dispatcher thread
	if (!outputBuffer) {
		outputBuffer = OutputMessagePool::getOutputMessage();
	} else if ((outputBuffer->getLength() + size) > NetworkMessage::MAX_PROTOCOL_BODY_LENGTH) {
		send(outputBuffer);
		outputBuffer = OutputMessagePool::getOutputMessage();
	}
	return outputBuffer;
}

void Protocol::XTEA_encrypt(OutputMessage& msg) const
{
	const uint32_t delta = 0x61C88647;

	// The message must be a multiple of 8
	size_t paddingBytes = msg.getLength() % 8;
	if (paddingBytes != 0) {
		msg.addPaddingBytes(8 - paddingBytes);
	}

	uint8_t* buffer = msg.getOutputBuffer();
	const size_t messageLength = msg.getLength();
	size_t readPos = 0;
	const uint32_t k[] = {key[0], key[1], key[2], key[3]};
	while (readPos < messageLength) {
		uint32_t v0;
		memcpy(&v0, buffer + readPos, 4);
		uint32_t v1;
		memcpy(&v1, buffer + readPos + 4, 4);

		uint32_t sum = 0;

		for (int32_t i = 32; --i >= 0;) {
			v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
			sum -= delta;
			v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[(sum >> 11) & 3]);
		}

		memcpy(buffer + readPos, &v0, 4);
		readPos += 4;
		memcpy(buffer + readPos, &v1, 4);
		readPos += 4;
	}
}

bool Protocol::XTEA_decrypt(NetworkMessage& msg) const
{
	if (((msg.getLength() - 6) & 7) != 0) {
		return false;
	}

	const uint32_t delta = 0x61C88647;

	uint8_t* buffer = msg.getBuffer() + msg.getBufferPosition();
	const size_t messageLength = (msg.getLength() - 6);
	size_t readPos = 0;
	const uint32_t k[] = {key[0], key[1], key[2], key[3]};
	while (readPos < messageLength) {
		uint32_t v0;
		memcpy(&v0, buffer + readPos, 4);
		uint32_t v1;
		memcpy(&v1, buffer + readPos + 4, 4);

		uint32_t sum = 0xC6EF3720;

		for (int32_t i = 32; --i >= 0;) {
			v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[(sum >> 11) & 3]);
			sum += delta;
			v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
		}

		memcpy(buffer + readPos, &v0, 4);
		readPos += 4;
		memcpy(buffer + readPos, &v1, 4);
		readPos += 4;
	}

	int innerLength = msg.get<uint16_t>();
	if (innerLength > msg.getLength() - 8) {
		return false;
	}

	msg.setLength(innerLength);
	return true;
}

bool Protocol::RSA_decrypt(NetworkMessage& msg)
{
	if ((msg.getLength() - msg.getBufferPosition()) < 128) {
		return false;
	}

	g_RSA.decrypt(reinterpret_cast<char*>(msg.getBuffer()) + msg.getBufferPosition()); //does not break strict aliasing
	return msg.getByte() == 0;
}

uint32_t Protocol::getIP() const
{
	if (auto connection = getConnection()) {
		return connection->getIP();
	}

	return 0;
}


@@ -12,6 +12,10 @@
 #include "storage/test/TestUtils.h"
 #include "fs/TempDir.h"

+DECLARE_int32(load_data_interval_secs);
+DECLARE_int32(heartbeat_interval_secs);
+DECLARE_uint32(heartbeat_interval);
+
 namespace nebula {
 namespace meta {

/* Copyright (c) 2019 vesoft inc. All rights reserved.
 *
 * This source code is licensed under Apache 2.0 License,
 * attached with Common Clause Condition 1.0, found in the LICENSES directory.
 */
#include "base/Base.h"
#include <gtest/gtest.h>
#include <folly/executors/CPUThreadPoolExecutor.h>
#include <folly/synchronization/Baton.h>
#include "meta/processors/admin/Balancer.h"
#include "meta/test/TestUtils.h"
#include "storage/test/TestUtils.h"
#include "fs/TempDir.h"

namespace nebula {
namespace meta {

TEST(BalanceIntegrationTest, SimpleTest) {
    auto sc = std::make_unique<test::ServerContext>();
    auto handler = std::make_shared<nebula::storage::StorageServiceHandler>(nullptr, nullptr);
    sc->mockCommon("storage", 0, handler);
    LOG(INFO) << "Start storage server on " << sc->port_;
}

}  // namespace meta
}  // namespace nebula

int main(int argc, char** argv) {
    testing::InitGoogleTest(&argc, argv);
    folly::init(&argc, &argv, true);
    google::SetStderrLogging(google::INFO);
    return RUN_ALL_TESTS();
}


@@ -74,7 +74,7 @@ TraverseExecutor::makeTraverseExecutor(Sentence *sentence, ExecutionContext *ect
     return executor;
 }

-void Collector::collect(VariantType &var, RowWriter *writer) const {
+Status Collector::collect(VariantType &var, RowWriter *writer) {
     switch (var.which()) {
         case VAR_INT64:
             (*writer) << boost::get<int64_t>(var);

/* Copyright (c) 2018 vesoft inc. All rights reserved.
 *
 * This source code is licensed under Apache 2.0 License,
 * attached with Common Clause Condition 1.0, found in the LICENSES directory.
 */

#include "base/Base.h"
#include "graph/TraverseExecutor.h"
#include "parser/TraverseSentences.h"
#include "graph/GoExecutor.h"
#include "graph/PipeExecutor.h"
#include "graph/OrderByExecutor.h"
#include "graph/FetchVerticesExecutor.h"
#include "graph/FetchEdgesExecutor.h"
#include "dataman/RowReader.h"
#include "dataman/RowWriter.h"
#include "graph/SetExecutor.h"
#include "graph/FindExecutor.h"
#include "graph/MatchExecutor.h"
#include "graph/FindPathExecutor.h"
#include "graph/LimitExecutor.h"

namespace nebula {
namespace graph {

std::unique_ptr<TraverseExecutor> TraverseExecutor::makeTraverseExecutor(Sentence *sentence) {
    return makeTraverseExecutor(sentence, ectx());
}

// static
std::unique_ptr<TraverseExecutor>
TraverseExecutor::makeTraverseExecutor(Sentence *sentence, ExecutionContext *ectx) {
    auto kind = sentence->kind();
    std::unique_ptr<TraverseExecutor> executor;
    switch (kind) {
        case Sentence::Kind::kGo:
            executor = std::make_unique<GoExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kPipe:
            executor = std::make_unique<PipeExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kOrderBy:
            executor = std::make_unique<OrderByExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kFetchVertices:
            executor = std::make_unique<FetchVerticesExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kFetchEdges:
            executor = std::make_unique<FetchEdgesExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kSet:
            executor = std::make_unique<SetExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kMatch:
            executor = std::make_unique<MatchExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kFind:
            executor = std::make_unique<FindExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kFindPath:
            executor = std::make_unique<FindPathExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kLimit:
            executor = std::make_unique<LimitExecutor>(sentence, ectx);
            break;
        case Sentence::Kind::kUnknown:
            LOG(FATAL) << "Sentence kind unknown";
            break;
        default:
            LOG(FATAL) << "Sentence kind illegal: " << kind;
            break;
    }
    return executor;
}

void Collector::collect(VariantType &var, RowWriter *writer) const {
    switch (var.which()) {
        case VAR_INT64:
            (*writer) << boost::get<int64_t>(var);
            break;
        case VAR_DOUBLE:
            (*writer) << boost::get<double>(var);
            break;
        case VAR_BOOL:
            (*writer) << boost::get<bool>(var);
            break;
        case VAR_STR:
            (*writer) << boost::get<std::string>(var);
            break;
        default:
            LOG(FATAL) << "Unknown VariantType: " << var.which();
    }
}

VariantType Collector::getProp(const std::string &prop,
                               const RowReader *reader) const {
    DCHECK(reader != nullptr);
    DCHECK(schema_ != nullptr);
    using nebula::cpp2::SupportedType;
    auto type = schema_->getFieldType(prop).type;
    switch (type) {
        case SupportedType::BOOL: {
            bool v;
            reader->getBool(prop, v);
            VLOG(3) << "get prop: " << prop << ", value: " << v;
            return v;
        }
        case SupportedType::TIMESTAMP:
        case SupportedType::INT: {
            int64_t v;
            reader->getInt(prop, v);
            VLOG(3) << "get prop: " << prop << ", value: " << v;
            return v;
        }
        case SupportedType::VID: {
            VertexID v;
            reader->getVid(prop, v);
            VLOG(3) << "get prop: " << prop << ", value: " << v;
            return v;
        }
        case SupportedType::FLOAT: {
            float v;
            reader->getFloat(prop, v);
            VLOG(3) << "get prop: " << prop << ", value: " << v;
            return static_cast<double>(v);
        }
        case SupportedType::DOUBLE: {
            double v;
            reader->getDouble(prop, v);
            VLOG(3) << "get prop: " << prop << ", value: " << v;
            return v;
        }
        case SupportedType::STRING: {
            folly::StringPiece v;
            reader->getString(prop, v);
            VLOG(3) << "get prop: " << prop << ", value: " << v;
            return v.toString();
        }
        default:
            LOG(FATAL) << "Unknown type: " << static_cast<int32_t>(type);
            return "";
    }
}

}   // namespace graph
}   // namespace nebula


@@ -103,7 +103,7 @@ rtps::Time_t::Time_t(

 int64_t rtps::Time_t::to_ns() const
 {
-    int64_t nano = seconds_ * 1000000000ULL;
+    int64_t nano = seconds_ * C_SECONDS;
     nano += nanosec_;
     return nano;
 }

// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file Time_t.cpp
 */
#include <fastrtps/rtps/common/Time_t.h>

using namespace eprosima::fastrtps;

Time_t::Time_t()
{
    seconds = 0;
    nanosec = 0;
}

Time_t::Time_t(
        int32_t sec,
        uint32_t nsec)
{
    seconds = sec;
    nanosec = nsec;
}

Time_t::Time_t(
        long double sec)
{
    seconds = static_cast<int32_t>(sec);
    nanosec = static_cast<uint32_t>((sec - seconds) * 1000000000ULL);
}

void Time_t::fraction(
        uint32_t frac)
{
    nanosec = (frac == 0xffffffff)
        ? 0xffffffff
        : static_cast<uint32_t>(std::lroundl(frac * rtps::FRACTION_TO_NANO));
}

uint32_t Time_t::fraction() const
{
    if (nanosec == 0xffffffff)
    {
        return nanosec;
    }

    uint32_t fraction = static_cast<uint32_t>(std::lroundl(nanosec * rtps::NANO_TO_FRACTION));
    uint32_t nano_check = static_cast<uint32_t>(std::lroundl(fraction * rtps::FRACTION_TO_NANO));
    while (nano_check != nanosec)
    {
        nano_check = static_cast<uint32_t>(std::lroundl(++fraction * rtps::FRACTION_TO_NANO));
    }

    return fraction;
}

int64_t Time_t::to_ns() const
{
    int64_t nano = seconds * 1000000000ULL;
    nano += nanosec;
    return nano;
}

rtps::Time_t::Time_t()
{
    seconds_ = 0;
    fraction_ = 0;
    nanosec_ = 0;
}

rtps::Time_t::Time_t(
        int32_t sec,
        uint32_t frac)
{
    seconds_ = sec;
    set_fraction(frac);
}

rtps::Time_t::Time_t(
        long double sec)
{
    seconds_ = static_cast<int32_t>(sec);
    set_fraction(static_cast<uint32_t>((sec - seconds_) * 4294967296ULL));
}

rtps::Time_t::Time_t(
        const eprosima::fastrtps::Time_t& time)
{
    seconds_ = time.seconds;
    set_nanosec(time.nanosec);
}

int64_t rtps::Time_t::to_ns() const
{
    int64_t nano = seconds_ * 1000000000ULL;
    nano += nanosec_;
    return nano;
}

int32_t rtps::Time_t::seconds() const
{
    return seconds_;
}

int32_t& rtps::Time_t::seconds()
{
    return seconds_;
}

void rtps::Time_t::seconds(
        int32_t sec)
{
    seconds_ = sec;
}

uint32_t rtps::Time_t::nanosec() const
{
    return nanosec_;
}

void rtps::Time_t::nanosec(
        uint32_t nanos)
{
    const uint32_t s_to_nano = 1000000000UL;
    if (nanos >= s_to_nano)
    {
        nanos %= s_to_nano; // Remove the seconds
    }
    set_nanosec(nanos);
}

uint32_t rtps::Time_t::fraction() const
{
    return fraction_;
}

void rtps::Time_t::fraction(
        uint32_t frac)
{
    set_fraction(frac);
}

Duration_t rtps::Time_t::to_duration_t() const
{
    return Duration_t(seconds_, nanosec_);
}

void rtps::Time_t::from_duration_t(const Duration_t& duration)
{
    seconds_ = duration.seconds;
    set_nanosec(duration.nanosec);
}

void rtps::Time_t::set_fraction(
        uint32_t frac)
{
    fraction_ = frac;
    nanosec_ = (fraction_ == 0xffffffff)
        ? 0xffffffff
        : static_cast<uint32_t>(std::lroundl(fraction_ * FRACTION_TO_NANO));
}

void rtps::Time_t::set_nanosec(
        uint32_t nanos)
{
    nanosec_ = nanos;
    fraction_ = (nanosec_ == 0xffffffff)
        ? 0xffffffff
        : static_cast<uint32_t>(std::lroundl(nanosec_ * NANO_TO_FRACTION));

    if (fraction_ != 0xffffffff)
    {
        uint32_t nano_check = static_cast<uint32_t>(std::lroundl(fraction_ * FRACTION_TO_NANO));
        while (nano_check != nanosec_)
        {
            nano_check = static_cast<uint32_t>(std::lroundl(++fraction_ * FRACTION_TO_NANO));
        }
    }
}


@@ -38,19 +38,24 @@ int main(int argc, char const *argv[]) {
                                  .set_max_iteration_count(5)
                                  .set_accuracy_threshold(0.001);

-    const auto result_train = dal::train(kmeans_desc, x_train, initial_centroids);
+    #ifdef MPI
+        oneapi::dal::network::mpi::network net;
+    #else
+        oneapi::dal::network::empty_network net;
+    #endif

-    std::cout << "Iteration count: " << result_train.get_iteration_count() << std::endl;
-    std::cout << "Objective function value: " << result_train.get_objective_function_value()
+    const auto result_train = dal::train(kmeans_desc, x_train, initial_centroids, net);
+
+    std::cout << "[" << myRank << "]" << "Iteration count: " << result_train.get_iteration_count() << std::endl;
+    std::cout << "[" << myRank << "]" << "Objective function value: " << result_train.get_objective_function_value()
               << std::endl;
-    std::cout << "Lables:" << std::endl << result_train.get_labels() << std::endl;
-    std::cout << "Centroids:" << std::endl << result_train.get_model().get_centroids() << std::endl;
+    std::cout << "[" << myRank << "]" << "Centroids:" << std::endl << result_train.get_model().get_centroids() << std::endl;

     const auto result_test = dal::infer(kmeans_desc, result_train.get_model(), x_test);

-    std::cout << "Infer result:" << std::endl << result_test.get_labels() << std::endl;
-
-    std::cout << "Ground truth:" << std::endl << y_test << std::endl;
+    #ifdef MPI
+        MPI_Finalize();
+    #endif

     return 0;
 }

/*******************************************************************************
* Copyright 2020 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#include "example_util/utils.hpp"
#include "oneapi/dal/algo/kmeans.hpp"
#include "oneapi/dal/io/csv.hpp"

using namespace oneapi;

int main(int argc, char const *argv[]) {
    const std::string train_data_file_name        = get_data_path("kmeans_dense_train_data.csv");
    const std::string initial_centroids_file_name = get_data_path("kmeans_dense_train_centroids.csv");
    const std::string test_data_file_name         = get_data_path("kmeans_dense_test_data.csv");
    const std::string test_label_file_name        = get_data_path("kmeans_dense_test_label.csv");

    const auto x_train           = dal::read<dal::table>(dal::csv::data_source{train_data_file_name});
    const auto initial_centroids = dal::read<dal::table>(dal::csv::data_source{initial_centroids_file_name});

    const auto x_test = dal::read<dal::table>(dal::csv::data_source{test_data_file_name});
    const auto y_test = dal::read<dal::table>(dal::csv::data_source{test_label_file_name});

    const auto kmeans_desc = dal::kmeans::descriptor<>()
                                 .set_cluster_count(20)
                                 .set_max_iteration_count(5)
                                 .set_accuracy_threshold(0.001);

    const auto result_train = dal::train(kmeans_desc, x_train, initial_centroids);

    std::cout << "Iteration count: " << result_train.get_iteration_count() << std::endl;
    std::cout << "Objective function value: " << result_train.get_objective_function_value()
              << std::endl;
    std::cout << "Lables:" << std::endl << result_train.get_labels() << std::endl;
    std::cout << "Centroids:" << std::endl << result_train.get_model().get_centroids() << std::endl;

    const auto result_test = dal::infer(kmeans_desc, result_train.get_model(), x_test);

    std::cout << "Infer result:" << std::endl << result_test.get_labels() << std::endl;

    std::cout << "Ground truth:" << std::endl << y_test << std::endl;

    return 0;
}


@@ -123,17 +123,11 @@ except ImportError:
     default_client = None
     wait = None

-    class dask_Array:
+    class dask_Array: # type: ignore
         """Dummy class for dask.array.Array."""

-        pass
-
-    class dask_DataFrame:
+    class dask_DataFrame: # type: ignore
         """Dummy class for dask.dataframe.DataFrame."""

-        pass
-
-    class dask_Series:
+    class dask_Series: # type: ignore
         """Dummy class for dask.dataframe.Series."""
-
-        pass

# coding: utf-8
"""Compatibility library."""

"""pandas"""
try:
    from pandas import DataFrame as pd_DataFrame
    from pandas import Series as pd_Series
    from pandas import concat
    from pandas.api.types import is_sparse as is_dtype_sparse
    PANDAS_INSTALLED = True
except ImportError:
    PANDAS_INSTALLED = False

    class pd_Series:
        """Dummy class for pandas.Series."""

        pass

    class pd_DataFrame:
        """Dummy class for pandas.DataFrame."""

        pass

    concat = None
    is_dtype_sparse = None

"""matplotlib"""
try:
    import matplotlib
    MATPLOTLIB_INSTALLED = True
except ImportError:
    MATPLOTLIB_INSTALLED = False

"""graphviz"""
try:
    import graphviz
    GRAPHVIZ_INSTALLED = True
except ImportError:
    GRAPHVIZ_INSTALLED = False

"""datatable"""
try:
    import datatable
    if hasattr(datatable, "Frame"):
        dt_DataTable = datatable.Frame
    else:
        dt_DataTable = datatable.DataTable
    DATATABLE_INSTALLED = True
except ImportError:
    DATATABLE_INSTALLED = False

    class dt_DataTable:
        """Dummy class for datatable.DataTable."""

        pass

"""sklearn"""
try:
    from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
    from sklearn.preprocessing import LabelEncoder
    from sklearn.utils.class_weight import compute_sample_weight
    from sklearn.utils.multiclass import check_classification_targets
    from sklearn.utils.validation import assert_all_finite, check_array, check_X_y
    try:
        from sklearn.exceptions import NotFittedError
        from sklearn.model_selection import GroupKFold, StratifiedKFold
    except ImportError:
        from sklearn.cross_validation import GroupKFold, StratifiedKFold
        from sklearn.utils.validation import NotFittedError
    try:
        from sklearn.utils.validation import _check_sample_weight
    except ImportError:
        from sklearn.utils.validation import check_consistent_length

        # dummy function to support older version of scikit-learn
        def _check_sample_weight(sample_weight, X, dtype=None):
            check_consistent_length(sample_weight, X)
            return sample_weight

    SKLEARN_INSTALLED = True
    _LGBMModelBase = BaseEstimator
    _LGBMRegressorBase = RegressorMixin
    _LGBMClassifierBase = ClassifierMixin
    _LGBMLabelEncoder = LabelEncoder
    LGBMNotFittedError = NotFittedError
    _LGBMStratifiedKFold = StratifiedKFold
    _LGBMGroupKFold = GroupKFold
    _LGBMCheckXY = check_X_y
    _LGBMCheckArray = check_array
    _LGBMCheckSampleWeight = _check_sample_weight
    _LGBMAssertAllFinite = assert_all_finite
    _LGBMCheckClassificationTargets = check_classification_targets
    _LGBMComputeSampleWeight = compute_sample_weight
except ImportError:
    SKLEARN_INSTALLED = False
    _LGBMModelBase = object
    _LGBMClassifierBase = object
    _LGBMRegressorBase = object
    _LGBMLabelEncoder = None
    LGBMNotFittedError = ValueError
    _LGBMStratifiedKFold = None
    _LGBMGroupKFold = None
    _LGBMCheckXY = None
    _LGBMCheckArray = None
    _LGBMCheckSampleWeight = None
    _LGBMAssertAllFinite = None
    _LGBMCheckClassificationTargets = None
    _LGBMComputeSampleWeight = None

"""dask"""
try:
    from dask import delayed
    from dask.array import Array as dask_Array
    from dask.dataframe import DataFrame as dask_DataFrame
    from dask.dataframe import Series as dask_Series
    from dask.distributed import Client, default_client, wait
    DASK_INSTALLED = True
except ImportError:
    DASK_INSTALLED = False
    delayed = None
    Client = object
    default_client = None
    wait = None

    class dask_Array:
        """Dummy class for dask.array.Array."""

        pass

    class dask_DataFrame:
        """Dummy class for dask.dataframe.DataFrame."""

        pass

    class dask_Series:
        """Dummy class for dask.dataframe.Series."""

        pass


@@ -146,7 +146,7 @@ def _HeightFirstSplit(cluster, n):
   if len(cluster) == n:
     return cluster.GetPoints()
   clusters = [cluster]
-  for i in range(n - 1):
+  for _ in range(n - 1):
     nxtIdx = 0
     while nxtIdx < len(clusters) and len(clusters[nxtIdx]) == 1:
       nxtIdx += 1

# $Id$
#
# Copyright (C) 2001-2008  greg Landrum
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#
"""utility functions for clustering

"""

def GetNodeList(cluster):
  """returns an ordered list of all nodes below cluster

  the ordering is done using the lengths of the child nodes

   **Arguments**

     - cluster: the cluster in question

   **Returns**

     - a list of the leaves below this cluster

  """
  if len(cluster) == 1:
    return [cluster]
  else:
    children = cluster.GetChildren()
    children.sort(key=lambda x: len(x), reverse=True)
    res = []
    for child in children:
      res += GetNodeList(child)
    res += [cluster]
    return res

def GetNodesDownToCentroids(cluster, above=1):
  """returns an ordered list of all nodes below cluster

  """
  if hasattr(cluster, '_isCentroid'):
    cluster._aboveCentroid = 0
    above = -1
  else:
    cluster._aboveCentroid = above
  if len(cluster) == 1:
    return [cluster]
  else:
    res = []
    children = cluster.GetChildren()
    children.sort(lambda x, y: cmp(len(y), len(x)))
    for child in children:
      res = res + GetNodesDownToCentroids(child, above)
    res = res + [cluster]
    return res

def FindClusterCentroidFromDists(cluster, dists):
  """ find the point in a cluster which has the smallest summed
     Euclidean distance to all others

   **Arguments**

     - cluster: the cluster to work with

     - dists: the distance matrix to use for the points

   **Returns**

     - the index of the centroid point

  """
  children = cluster.GetPoints()
  pts = [x.GetData() for x in children]

  best = 1e24
  bestIdx = -1
  for pt in pts:
    dAccum = 0.0
    # loop over others and add'em up
    for other in pts:
      if other != pt:
        if other > pt:
          row, col = pt, other
        else:
          row, col = other, pt
        dAccum += dists[col * (col - 1) / 2 + row]
        if dAccum >= best:
          # minor efficiency hack
          break
    if dAccum < best:
      best = dAccum
      bestIdx = pt
  for i in range(len(pts)):
    pt = pts[i]
    if pt != bestIdx:
      if pt > bestIdx:
        row, col = bestIdx, pt
      else:
        row, col = pt, bestIdx
      children[i]._distToCenter = dists[col * (col - 1) / 2 + row]
    else:
      children[i]._distToCenter = 0.0
    children[i]._clustCenter = bestIdx
  cluster._clustCenter = bestIdx
  cluster._distToCenter = 0.0

  return bestIdx

def _BreadthFirstSplit(cluster, n):
  """  *Internal Use Only*

  """
  if len(cluster) < n:
    raise ValueError('Cannot split cluster of length %d into %d pieces' % (len(cluster), n))
  if len(cluster) == n:
    return cluster.GetPoints()
  clusters = [cluster]
  nxtIdx = 0
  for i in range(n - 1):
    while nxtIdx < len(clusters) and len(clusters[nxtIdx]) == 1:
      nxtIdx += 1
    assert nxtIdx < len(clusters)

    children = clusters[nxtIdx].GetChildren()
    children.sort(key=lambda x: x.GetMetric(), reverse=True)
    for child in children:
      clusters.append(child)
    del clusters[nxtIdx]
  return clusters

def _HeightFirstSplit(cluster, n):
  """  *Internal Use Only*

  """
  if len(cluster) < n:
    raise ValueError('Cannot split cluster of length %d into %d pieces' % (len(cluster), n))
  if len(cluster) == n:
    return cluster.GetPoints()
  clusters = [cluster]
  for i in range(n - 1):
    nxtIdx = 0
    while nxtIdx < len(clusters) and len(clusters[nxtIdx]) == 1:
      nxtIdx += 1
    assert nxtIdx < len(clusters)

    children = clusters[nxtIdx].GetChildren()
    for child in children:
      clusters.append(child)
    del clusters[nxtIdx]
    clusters.sort(key=lambda x: x.GetMetric(), reverse=True)
  return clusters

def SplitIntoNClusters(cluster, n, breadthFirst=1):
  """  splits a cluster tree into a set of branches

    **Arguments**

      - cluster: the root of the cluster tree

      - n: the number of clusters to include in the split

      - breadthFirst: toggles breadth first (vs depth first) cleavage
        of the cluster tree.

    **Returns**

      - a list of sub clusters

  """
  if breadthFirst:
    return _BreadthFirstSplit(cluster, n)
  else:
    return _HeightFirstSplit(cluster, n)


@@ -147,7 +147,7 @@ int main() {
         for (int t = 0; t < TEST_ITERS; t++) {
             hipEventRecord(start);
             for (int i = 0; i < DISPATCHES_PER_TEST; i++) {
-                hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream, Ad);
+                hipExtLaunchKernelGGL((EmptyKernel), dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream, start, stop, 0);
             }
             stopTest(start, stop, "StreamASyncDispatchNoWait", DISPATCHES_PER_TEST);
         }

/*
Copyright (c) 2015-present Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "hip/hip_runtime.h"
#include <iostream>
#include <time.h>
#include "ResultDatabase.h"

#define PRINT_PROGRESS 0

#define check(cmd)                                                                                \
    {                                                                                \
        hipError_t status = cmd;                                                                   \
        if (status != hipSuccess) {                                                                \
            printf("error: '%s'(%d) from %s at %s:%d\n", hipGetErrorString(status), status, #cmd,  \
                   __FILE__, __LINE__);                                                            \
            abort();                                                                               \
        }                                                                                \
    }

#define LEN 1024 * 1024

#define NUM_GROUPS 1
#define GROUP_SIZE 64
#define TEST_ITERS 20
#define DISPATCHES_PER_TEST 100

const unsigned p_tests = 0xfffffff;

// HCC optimizes away fully NULL kernel calls, so run one that is nearly null:
__global__ void NearlyNull(float* Ad) {
    if (Ad) {
        Ad[0] = 42;
    }
}

ResultDatabase resultDB;

void stopTest(hipEvent_t start, hipEvent_t stop, const char* msg, int iters) {
    float mS = 0;
    check(hipEventRecord(stop));
    check(hipDeviceSynchronize());
    check(hipEventElapsedTime(&mS, start, stop));
    resultDB.AddResult(std::string(msg), "", "uS", mS * 1000 / iters);
    if (PRINT_PROGRESS & 0x1) {
        std::cout << msg << "\t\t" << mS * 1000 / iters << " uS" << std::endl;
    }
    if (PRINT_PROGRESS & 0x2) {
        resultDB.DumpSummary(std::cout);
    }
}

int main() {
    hipError_t err;
    float* Ad;
    check(hipMalloc(&Ad, 4));

    hipStream_t stream;
    check(hipStreamCreate(&stream));

    hipEvent_t start, sync, stop;
    check(hipEventCreate(&start));
    check(hipEventCreateWithFlags(&sync, hipEventBlockingSync));
    check(hipEventCreate(&stop));

    hipStream_t stream0 = 0;

    if (p_tests & 0x1) {
        hipEventRecord(start);
        hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream0, Ad);
        stopTest(start, stop, "FirstKernelLaunch", 1);
    }

    if (p_tests & 0x2) {
        hipEventRecord(start);
        hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream0, Ad);
        stopTest(start, stop, "SecondKernelLaunch", 1);
    }

    if (p_tests & 0x4) {
        for (int t = 0; t < TEST_ITERS; t++) {
            hipEventRecord(start);
            for (int i = 0; i < DISPATCHES_PER_TEST; i++) {
                hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream0, Ad);
                hipEventRecord(sync);
                hipEventSynchronize(sync);
            }
            stopTest(start, stop, "NullStreamASyncDispatchWait", DISPATCHES_PER_TEST);
        }
    }

    if (p_tests & 0x10) {
        for (int t = 0; t < TEST_ITERS; t++) {
            hipEventRecord(start);
            for (int i = 0; i < DISPATCHES_PER_TEST; i++) {
                hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream, Ad);
                hipEventRecord(sync);
                hipEventSynchronize(sync);
            }
            stopTest(start, stop, "StreamASyncDispatchWait", DISPATCHES_PER_TEST);
        }
    }

#if 1

    if (p_tests & 0x40) {
        for (int t = 0; t < TEST_ITERS; t++) {
            hipEventRecord(start);
            for (int i = 0; i < DISPATCHES_PER_TEST; i++) {
                hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream0, Ad);
            }
            stopTest(start, stop, "NullStreamASyncDispatchNoWait", DISPATCHES_PER_TEST);
        }
    }

    if (p_tests & 0x80) {
        for (int t = 0; t < TEST_ITERS; t++) {
            hipEventRecord(start);
            for (int i = 0; i < DISPATCHES_PER_TEST; i++) {
                hipLaunchKernelGGL(NearlyNull, dim3(NUM_GROUPS), dim3(GROUP_SIZE), 0, stream, Ad);
            }
            stopTest(start, stop, "StreamASyncDispatchNoWait", DISPATCHES_PER_TEST);
        }
    }
#endif
    resultDB.DumpSummary(std::cout);

    check(hipEventDestroy(start));
    check(hipEventDestroy(sync));
    check(hipEventDestroy(stop));
}